Table View Cell Segues not Working - objective-c

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];

Related

Different reuseIdentifier in xib and by registration, what are the side effects?

In my xib, I set the reuse identifier of the cell to A
but then in -registerNib:forCellReuseIdentifier: I declare "B"
[self.tableView registerNib:[UINib nibWithNibName:#"A" bundle:nil]
forCellReuseIdentifier:#"B"];
what are the effects of such behaviour? all looks running ok, why there are 2 places to enter the reuse identifier?
You can safely ignore the reuseIdentifier field in the xib.
Because that option is actually pretty useless when you are dealing with xibs. It's probably there just because Xcode uses the exact same User Interface as it uses when editing storyboards.
Compared to storyboard, in xibs there is no connection between the cell and the tableView. The cell is even stored in a different file. So the cell won't, and can't be dequeued/created with its reuseIdentifier.
If you have a cell in a xib you have to register it in code. This process creates the connection between the tableView and the cell (or its xib).

Segue from Custom Cell (dynamic Prototype Cell)

Im using the Storyboard to create my UI.
I have a SplitViewController. The MasterViewController holds a TableViewController where I created a CustomCell with Custom Design.
The Cells are shown pretty good with my data.
The point is: The Custom Cell also holds an Info-Button, with should Popup a little 300x88 TableViewController with some data.
If the Cells in my MasterViewController->TableViewController where a Static one, I just would drag & drop a Segue from the Info-Button to my Popup-TableViewController.
But sadly I cant do this with a dynamic Prototype Cell... I just get the error:
Couldn't compile connections ... >> anchor >> UIButton...
So how can I implement this?
Kind Regards.
Define a manual segue dragging the whole master view onto the detail view, then add a manual target/action to your custom info button and perform the manual segue there. Of course you must set a segue identifier in the storyboard to later reference it in the code, IB will tell you if you forget to.
Instead of connecting and creating segue from individual cells, you can connect all those segues from the View Controller button, lying below your view in your storyboard. In this case, you will have multiple segues and none of them will be individually connected to cells. And when segues are ready, you can use this method for moving on to the next View Controller depending on which cell is tapped from the tableView.
[self performSegueWithIdentifier:#"yourSegue" sender:nil];
You just have to check which cell is tapped and then perform the appropriate Segue on tap using the above code and the respective segue identifier. The prepareForSegue method will be called immediately after this method is called.
Look out for the yellow button as shown below your View Screen in your storyboard.
Drag and drop a segue from that button onto the View Controller that you want to connect.
Hope this helps.

UITableViewCell setEditing is not called

I have UIViewController that contains UITableView. I implemented my custom edit button that toggles setEditing:animated for the table view. Everything works good, delete and reordering icons show up. Table view cells has custom class (subclass of UITableViewCell), where I override setEditing method to do some extra customisation when edit mode is ON.
From documentation:
When the table view receives setEditing:animated:, it sends the same
message to the UITableViewCell object for each visible row.
As I understand setEditing method should be called for every cell, but unfortunately it does not happen. Where could be the problem? What I am missing?
I found my mistake. I was overriding
-(void)setEditing:(BOOL)editing
but instead I should override
-(void)setEditing:(BOOL)editing animated:(BOOL)animated

Using "tableView didSelectRowAtIndexPath" in tandem with prepareForSegue

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];

UITableView within a UITableViewCell

I need to load a UITableViewCell xib into the root controller of a split view. The UITableViewCell contains a UITableView as a subview (designed in Interface Builder). This UITableView will be used to load other cells. Is the loading of the UITableView within the UITableViewCell possible to achieve??
In short - a cell with a tableview as a subview that loads data from DB.
I have tried this but with little success. The problem is that the delegate method...
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
..placed within the UITableViewCell implementation file, never gets triggered.
Please help.
Your design may be ill advised, however...
If you have code for your UITableViewCell class, then that object needs to be the data source and delegate for your embedded UITableView. Something, certainly, has to be the tableView's data source and delegate, of course.
Another best practice, in this case, would certainly to be to make sure that the tableView argument in the various data source and delegate methods is the tableView object you think it is before you do anything.
How many custom table cells with embedded table views are you planning? Why not use a single table view with Grouped style? Eacn section of the table can then contain the content that you are now putting into your UITableViewCell/UITableView element. Would certainly be simpler, and might achieve the same result.