Using "tableView didSelectRowAtIndexPath" in tandem with prepareForSegue - objective-c

I have a project utilizing storyboards that uses UITableView in tandem with a Navigation Controller. Its layout is similar to apple's iOS Address Book where there is a table of objects, and clicking on a cell pushes a view onto the navcontroller with that objects's details (properties). I am having trouble using the prepareForSegue method in harmony with the table view's didSelectRowAtIndex. I need a way for the prepareForSegue to know about the row passed in didSelectRowAtIndex so i can pass it's properties to the detail view controller being pushed since prepareForSegue gets called before didSelectRowAtIndex does. If possible I would still like to use the storyboard segue but if there isn't a way i can progamatically push/pop. There is another question similar to this on stackoverflow but it never really was answered, it kind of just rambled on.
If you know a work-around please let me know, thank you!

You can not use didSelectRowAtIndexPath: at all. You can get the index path in prepareForSegue: like this:
UITableViewCell *cell = (UITableViewCell*) sender;
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];

Related

Use single UITableViewCell subclass across multiple UIViewControllers

So I have this UITableViewCell subclass that has some really complicated logics in it - it triggers some actions in UIViewController it's actually attached to. Of course the cell is not aware of its UIViewController but I still navigate to it like this:
UITabBarController *tabVC = (UITabBarController *)appDelegate.rootVC.centerPanel;
SGFirstTabViewController *firstTab1 = [tabVC.viewControllers firstObject];
[firstTab1 reloadCell:self];
The thing is now that I want to use the same subclass of UITableViewCell around about 5 different UIViewControllers.
What's the best way to do this? I will almost never know what is the UITableViewCell's VC is and I simply can't create 5 different cell subclasses with the same code over and over. What's the best way around it?
I think there is perhaps some misunderstanding in the role of the cell in the model-view-controller programming pattern.
The cell should not reload itself but it should be told to reload by the view controller that controls it. The reload code in the cell can stay the same.
So, rather than the cell having to find out which one its view controller is, have the view controller listen to the cell action (e.g. via delegate methods) and fill it with the appropriate reload data as directed.

Master Detail Scene, Detail scene wont load properly

im following this ios tutorial:
http://developer.apple.com/library/ios/#documentation/iPhone/Conceptual/SecondiOSAppTutorial/CustomizingDetailView/CustomizingDetailView.html#//apple_ref/doc/uid/TP40011318-CH5-SW3
and im creating a master scene and a detail scene. they are both done as described in the tutorial.
Right now the problem is that when i enter the detail scene, the layout of detail scene is not loaded. instead, a master scene layout with nothing on it is loaded.
So far from what i can see, the problem has to do with transferring the data from master scene to detail scene.
The tutorial does not include this method:
(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
without it, the app couldnt go from master scene to detail scene. i think the segue method was suppose to transfer the data and make it go to detail scene, but for some reason it doesnt. so this method was edited to
{
BirdSighting *sightingAtIndex = [self.dataController objectInListAtIndex:indexPath.row];
BirdsDetailViewController *bd= [[BirdsDetailViewController alloc] init];
bd.sighting = sightingAtIndex;
[self.navigationController pushViewController:bd animated:YES];
}
and now the app can go from master scene to detail scene, but the detail scene looks like a master scene with nothing on it.
Any help regarding this or the transferring data between the scenes are greatly appreciated. Thank you in advance.
The point is that the table view selection method doesn't need to be implemented because the selection automatically triggers the segue. If you follow the guide in detail and to the end you will configure the segue and the handling of the segue trigger to configure the new detail view before it appears on screen.
the problem seem to come down to prepareForSegue isnt being called, and just using didselectrowatindexpath isnt transferring the data correctly.
so you can fix it by looking at this answer here:
Custom UITableViewCell not calling prepareForSegue
Seems like when user touch a cell, didSelectRowAtIndex is called, but prepareForSegue isnt. So you need to manually call performSegue for prepareForSegue to work? this is my interpretation.

Table View Cell Segues not Working

I'm trying to do something very simple: set up a segue so that when you click on a cell in a table it will take you to another View Controller. The problem, I believe, originates from the fact that the TableView these cells are in is embedded in a regular ViewController (as opposed to a TableViewController), and is one of two subviews in this ViewController.
As far as I can tell, I've set everything up correctly: I embedded the ViewController with the two subviews in a Navigation Contoller, set it to be the dataSource and delegate for the TableView, and created a push segue from a TableViewCell to my second View Controller in the storyboard. However, when the app is run and a user clicks a row in the table, it merely selects the row and the segue doesn't fire at all (I've debugged it and the prepareForSegue function isn't even being called).
Is there something I'm missing here? Or is this for some reason not possible if the TableView is not the only view in its view controller?
I find that if I had recently wired the segue from the cell's accessory view, and later delete the segue and try to wire a new segue to the cell directly it does not work (using Xcode 4.6.2) -- Xcode keeps connecting the segue to the accessory view (even if there isn't one)! The way I fixed it is by selecting the cell in IB and using the connection inspector to (1) delete the original "accessory action" segue and (2) directly wire the "selection" segue by dragging from the filled circle in the image below to the appropriate destination view controller.
This may or may not help you, but I ran into this issue because I had defined two different cell types, and had provided didSelectRowAtIndexPath implementation. I had to add [self performSegueWithIdentifier:#"Whatever" sender:self] as part of didSelectRowAtIndexPath and the issue got resolved.
If you are able to at least detect row selections, you may be able to take advantage of this method.
If you have customized the cell rendering, e.g.:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
then ensure you are using the same cell identifier as specified for the prototype cell in ste story.
So in this case the 'Identifier' of the cell which hooked up the segue should be set to 'Cell'.
If you are creating the StoryBoard in Xcode then do the following:
Create a UITableViewController
Add a prototype UITableViewCell
Create the UIViewController that will be your segue target.
Control-Click on the prototype UITableViewCell and drag to the segue target.
That's it. You'll probably want to edit the characteristics of the UITableViewCell to be, for example, your subclass of UITableViewCell.
I had the similar issue.
Fix is
1. Write SegueIdentifier for the segue in Storyboard
2. Add the following line [self performSegueWithIdentifier:#"SegueIdentifier" sender:nil]; in didSelectRowAtIndexPath.
I hope this would help.
Had the same problem. Segue from a prototype table view cell inside a regular View Controller would make the app crash. In case someone have the same problem.
My solution:
If you are using a push segue make sure your first View Controller with the table view is embedded in a Navigation Controller otherwise "push segues" wont work. Also point the segue at the destination View Controller, not its Navigation Controller!
And as mentioned earlier, make sure your "segue identifiers" are correct.
at "didSelectRowAtIndexPath" I call "[self performSegueWithIdentifier:#"Your_destination_View_Controller" sender:self];

how to implement didSelectRowAtIndexPath in table view in Xcode 4.2?

SliderDemoController *sliderDemoController=[[SliderDemoController alloc] initWithNibName:#"" bundle:nil];
[self.navigationController pushViewController:sliderDemoController animated:YES];
i implemented this method using storyboard. but this method is not working. i want to open new controller in table view cell.
If You want to access any view controllers which is in story board then you should use UIStoryBoard class.If you not sure about how to do that so, Here is the link for apple document about UIStoryBoard and use the method + (UIStoryboard *)storyboardWithName:(NSString *)name bundle:(NSBundle *) storyboardBundleOrNil to access your storyboard.
Access your view controller with the method - (id)instantiateViewControllerWithIdentifier:(NSString *)identifier and use the returned view controller object.Hope this helps you....
You don't need to query the UIStoryboard object -- setting up transitions between view controllers is what storyboards do automagically. And with storyboards, you don't need to implement tableView:didSelectRowAtIndexPath: either.
If you haven't already, define a segue from the (prototype) table cell to the destination view controller (looks like that's SliderDemoController for you) by control-dragging. (Choose the Push segue type.) For functionality equivalent to the non-storyboard code you posted, that's all there is to it -- now tapping any row in the table will push in a SliderDemoController.
However, you probably want to configure that SliderDemoController based on which row was selected, right? Then, in your table view controller, implement prepareForSegue:sender:. There you can get a reference to the destination view controller (from the segue parameter) and set it up however you like.

UITableView that links Cell to UIWebView NSURLRequest

My question seems simple enough and yet I have not been able to find an example to follow.
I have a UITableView with a list of courses that I want to link to their respective URLs.
There are 21 courses in the list and when the user scrolls the list, they select a course that then should link them to the web page (with a "back" button to return to the list).
I've found examples to link the URL directly from a button but not from a UITableView list.
How can you link the Cell in the TableView list to the NSURL Request to then load the web page?
Anyone with an answer or a link to a tutorial, would be very appreciated.
Check the delegate methods for UITableView:
http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UITableViewDelegate_Protocol/Reference/Reference.html
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
In your case you probably want to push another viewcontroller with a webview when the user taps a cell so you can allow the user to go back to the uitableview when he/she is done.
Make an data object called Link with two instance variables, the URLRequest object and a string that you want for your table cell to display.
When you set up your 21 objects, make 21 Link objects and store them in an array.
In your tableView:cellForRowAtIndexPath: method you will setup your row with the linkInsance.cellName value.
In your tableView:didSelectRowAtIndexPath: you will get the linkInstance.URLRequest and execute it.
You will get the link instances by getting an object by index in the array with the indexPath.row.
Use the standard "Navigation based application" template so that you've got a navigation controller in place. By default it is set up to use a table view controller as its root view controller, so it's a good starting point for you.
Now create a custom view controller subclass that embeds a UIWebView. Remember to create an IBOutlet for the webview and hook it up in Interface Builder. While you are in Interface Builder, select the main view and tell it you want a navigation bar at the top. Give your view controller an NSURL property too. In the loadView method for this class, tell the UIWebView to load that URL.
Now go back to the table view controller's delegate. In the tableView:didSelectRowAtIndexPath: method, create an autoreleased instance of your custom view controller. Assign the appropriate URL to the NSURL property that you just set up, then push it onto the navigation stack with [self.navigationController pushViewController:isAnimated:].