0

I have a segue set up between a UITableViewCell and a UIViewController. My understanding is that if I set up a Show segue from the prototype cell to a UIViewController, the UIViewController will be shown when tapping on one of the cells in my UITableView, but that's not happening for me. The only way to get it to work is to override the tableView didSelectRowAtIndexPath, and then call performSegueWithIdentifier.

To test this out, I threw together a quick project with a UITableViewController and a UIViewController. When I created a segue between the prototype cell and the UIViewController, it worked the way it should. So I know that it is possible, I'm just not sure what could be causing the problems in my actual project.

There's not much code to show for this. My prototype cell is a custom cell that comes from an xib file, but I also tried giving it a class of UITableViewCell, and it made no difference.

Has anyone ever run in to a case where a segue like this should work, but doesn't? If so, how did you fix it?

Dharmesh Dhorajiya
  • 3,976
  • 9
  • 30
  • 39
Scott Kilbourn
  • 1,545
  • 6
  • 18
  • 40
  • connect segue from Tableview – vijeesh Jul 01 '15 at 05:52
  • If your cell is being instantiated from a xib file, then you are not using a prototype cell in the storyboard. An then you have no segues at all linked from the cell as there are no segues in xib files. So, I think I am loosing something in your post. – Gabriel Jul 01 '15 at 06:01
  • Thank you for the comment. This was the key. Even though I changed my cell class back to UITableViewCell, I was still registering the nib. That was making it so that I had to use performSegueWithIdentifier. – Scott Kilbourn Jul 01 '15 at 15:23

1 Answers1

2

Let's clearify some essential things

  1. performSegueWithIdentifier: sender: is a method of UIViewController, so in order to perform segue from cell you need to declare weak property of your VC in cell, like this

@property (nonatomic, weak) UIViewController *segueController;

end perform you action via

[self.segueController performSegueWithIdentifier:@"segue_id" sender:self];
  1. The most common approach - using didSelectRowAtIndexPath has the following benefits: your segue logic belongs to controller, not to the view (because uitableviewcell is a child of uiview). so, i recommend you use this method for segues or manipulating with view hierarchy
Doro
  • 2,413
  • 2
  • 14
  • 26
  • I suggest to see also this : because you can see how to open the new ViewController and how to pass parameters to it – Lorenzo Jul 01 '15 at 06:23