prepareForSegue not called when UITableView row selected?

By | January 4, 2018
Questions:

I have setup a UITableView using a NSFetchedResultsController that displays a number of prototype UITableViewCells. I have hooked up a push segue from my UITableViewCell to my DetailViewController.

In my TableViewController I have implemented both:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"%s", __PRETTY_FUNCTION__);
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    NSLog(@"%s", __PRETTY_FUNCTION__);
}

When I run the application and click on a row in the tableView my application only calls:

[TableViewController tableView:didSelectRowAtIndexPath:]
[TableViewController tableView:didSelectRowAtIndexPath:]
[TableViewController tableView:didSelectRowAtIndexPath:]

If I delete the segue and rewire it form a UIBarButtonItem to the DetailViewController then the segue is called correctly, can anyone think what I am missing?

EDIT:

Added:

- (IBAction)tester:(id)sender {
    [self performSegueWithIdentifier:@"SEGTEST" sender:nil];
}

and connected it unto a UIButtonBarItem that called “tester” and it works, I just can’t get it to fire still when I link from the UITableViewCell to the controller.

Answers:

After a bit of digging I eventually found that the UITableViewCell identifier was set incorrectly to “CELL_ID” when it should have been “PLANTCELL_ID”. So for anyone else who finds that selecting a UITableViewCell in a UITableView does not call the associated segue then check your cell identifier is set correctly in both the storyboard and tableView:cellForRowAtIndexPath:

Questions:
Answers:

Rewire your segue from the ViewController itself and give it a name.
Then in the didSelectRow method call
[self performSegueWithIdentifier:@”yourSegueName”]
and see if it fires.

Leave a Reply

Your email address will not be published. Required fields are marked *