How to show view controller in UITableViewCell with UITableView - uitableview

i have UITableViewCell with tableView and i need show new controller if user select in my cell. But, navigationController and UIStoryboard doesn't work! How i can do this?

I think what you want to ask is after u tap a UITableViewCell , a new UIViewController would be pushed in. For that, you need to first embed in a navigationController to create a stack of the pushed view controllers. Then, inside tableView:didSelectRowAtIndexPath: add this :
YourController *yourController = [self.storyboard instantiateViewControllerWithIdentifier:NSStringFromClass([YourControllerStoryboardId class])];
[self.navigationController pushViewController:yourController animated:YES];
Check if you have set the delegate of your UITableView. Clean and Run your code.

Related

Can I change View to the ViewController when click the TableViewCell?

I am new to Objective-C.
It has TableViewCell and the TableView on the ViewController like the following picture.
I want to change the View to another Viewcontroller when I click the TableViewCell.
But it seems can not use the performSegueWithIdentifier to change the View in didSelectRowAtIndexPath without using UINavigationController , The error log like the following:
Push segues can only be used when the source controller is managed by an instance of UINavigationController.'
Is there has another way to change the View to another Viewcontroller when I click on the TableViewCell ?
Thanks in advance.
In Interface Builder(storyboard) click the following:
Exactly what the debugger says: Push segues can only be used when the source controller is managed by an instance of UINavigationController.' Then you'll be able to push segues.
U can present view controller using this
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
yourViewController *vc = [[yourViewController alloc]initWithNibName:#"yourViewController" bundle:[NSBundle mainBundle] ];
[self presentViewController:vc animated:YES completion:nil];
}

UITableViewController static cells not appearing when pushed to navigationController

I'm running into a rut...
I've create a UITableViewController with static cells (i've deleted all default UITableView methods). Whenever I segue to this view controller, the static cells appear, but when I push it onto the navigationcontroller the static cells do not appear...any idea to why this would be hapenning?
Here is my code:
//shows empty uitableviewcontroller
OthersUsersTableViewController *tvc = [[OthersUsersTableViewController alloc]init];
[self.navigationController pushViewController:tvc
animated:YES];
and
//works
[self performSegueWithIdentifier:#"toOtherUser" sender:self];
Thanks for the help!
If you manually alloc and init your Static table view, how should your app know that it is referencing a static table view in your storyboard?
Give your static table view an Identifier in the storyboard and initialize it like this:
OthersUsersTableViewController *tvc = [self.storyboard instantiateViewControllerWithIdentifier:#"staticTableView"];
You can set your identifier in the menu on the right where you also set the custom class for the view controller. The field is called Storyboard ID.
when you're just calling [[UIViewController alloc] init], the UI objects you've added and configured on the storyboard,does not get called or wired to your view controller.
You should instantiate the view controller through the storyboard itself by adding an identifier to your view controller(on the storyboard) and calling instantiateViewControllerWithIdentifier method of your storyboard.
You can get a reference to your storyboard through the current view controller's storyboard property.
OthersUsersTableViewController *tvc = [self.storyboard instantiateViewControllerWithIdentifier:#"identifier"];

Push a new view controller with navigation controller

So I created a new view controller which has the Custom class named DescriptionViewController.
And from my current view controller inside didSelectRowAtIndexPath I use this code:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
DescriptionViewController *descriptionViewController = [[DescriptionViewController alloc] initWithNibName:#"DescriptionViewController" bundle:nil];
[self.navigationController pushViewController:descriptionViewController animated:YES];
}
But nothing happens when I click on a cell. Is there anything more I need to do ?
If you put in a breakpoint and it is stopping in the didSelectRowAtIndexPath, my guess is that your self.navigationController is nil.
If you are using storyboards, make sure your entry point is a UINavigationController and not a UIViewController.
If you loading this view controller though a modal transition, make sure you push on a navigation controller with this view controller as the root view instead.
Select your viewcontroller in the storyboard and on top go to editor->embed in->navigation controller then instead of initWithNibName try just init

didDeselectRowAtIndexPath not called from UITableViewController added to UIViewController from story board

I am using a split view controller and wanting to add multiple table views to the master view controller.
So I can take advantage of prototype cells that (I believe) you can only get from UITableViewController I have put the UITableViewController into a second storyboard for the app. I am then substantiating it with:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"SecondStoryboard" bundle:nil];
MyTableViewController *myTableViewController = [storyboard instantiateInitialViewController];
I then add the view of this UITableViewController as a subview of my UIViewController.
[self.view addSubview:myTableViewController.view];
When I run this, I can select and highlight rows just fine. But didSelectRowAtIndexPath is not being called for some reason?
Thanks for any help,
Richard
Add this line
myTableViewController.view.delegate = myTableViewController;
or Make sure you have done the same in XIB(Interface Builder).
Turns out that I needed to add the uitableviewcontroller as a child controller of the uiviewcontroller as well. Just adding the view alone was not enough.

Storyboard Segue From View Controller to Itself

I am trying to make a mechanism to drill down a file / folder list. The idea is to show the same file list view controller every time the user selects a folder, and show a file detail view controller if he/she selects a file.
So far, I have created a segue from the file list view controller to the file detail view controller, and a segue from the file list table view cell to the the file list table view controller:
The issue with this is that as soon as the user taps the cell, the segue is executed. I would like to remove the segue from the table view cell and make one from the file list view controller to itself. That way, I could trigger the right segue programmatically when the user tapped the cell.
So, my question is: Is it possible to create a segue from a view controller to itself in Interface Builder?
If you are using a navigation controller you need to push the ViewController into the nav stack. In this example, i named my ViewController "VDI" in my Storyboard ID setting.
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle: nil];
YourVC *dest = [storyboard instantiateViewControllerWithIdentifier:#"VDI"];
[self.navigationController pushViewController:dest animated:YES];
If you don't want the NavigationController to keep adding itself into your "Back" history you can pop the stack before adding to it like so.
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle: nil];
YourVC *dest = [storyboard instantiateViewControllerWithIdentifier:#"VDI"];
UINavigationController *navController = self.navigationController;
[navController popViewControllerAnimated:NO];
[navController pushViewController:dest animated:YES];
Using Xcode 5 there is a much simpler solution.
Click the table cell in the storyboard
Open the Connections Inspector (right arrow icon in the upper right)
Under "triggered segues" you see "selection"
Drag from the circle next to "selection" to the cell in the storyboard
That's it.
I developed a method to create a segue using a phantom button. I believe it will solve your problem. You can read about it in my answer here.
Instead of performing a segue to the same controller, you can instantiate a view controller (the same one) from storyboard, and then push that onto the navigation controller.
Interface Builder approach: Just segue to a storyboard reference which refers back to the presenting view controller.
The correct answer is to use a Storyboard Reference that is referencing the UIViewController you want to segue to itself and then point the segue at it.
In IOS 6, there is a cleaner solution than using a phantom button. You can still define the segue from the table cell to the view controller, and look at the sender to cancel the automatically triggered segue:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//storyboards should use segues and override prepareForSegue instead
//but here we need custom logic to determine which segue to use
id item = [self.fetchedResultsController objectAtIndexPath:indexPath];
if (item meets condition) {
[self performSegueWithIdentifier:#"segue1" sender:self];
} else {
[self performSegueWithIdentifier:#"segue2" sender:self];
}
}
- (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender {
//ignore segue from cell since we we are calling manually in didSelectRowAtIndexPath
return (sender == self);
}
Here's how you can push another instance of the current view controller without defining a segue or hardcoding its own identifier:
SameViewController *same = [self.storyboard instantiateViewControllerWithIdentifier: self.restorationIdentifier];
[self.navigationController pushViewController: same animated: YES];
You just need to set the Restoration ID to be the same as Storyboard ID (there's a checkbox for that in IB).
Hope this helps.
I found that you can create multiple prototype cells.
Than you can link every cell (in the Storyboard) to a different View.
Something like this:
NSString *CellIdentifier = #"Cell";
if (Condition2 ){
CellIdentifier = #"Cell2"; }
if (Condition3 ){
CellIdentifier = #"Cell3"; }

Resources