Custom UITableViewCell not calling prepareForSegue

帅比萌擦擦* 提交于 2019-12-03 12:19:27

You need to implement

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {



[self performSegueWithIdentifier:@"YourSegueIdentifier" sender:nil];


}

You shouldn't have to implement didSelectRow for this to work - control-dragging from a cell to another view controller should create a segue that works when tapping the cell.

In my case, the problem was that the storyboard editor was creating the segue incorrectly. I had two very similar storyboards, one worked and the other didn't, and looking at the source code (right click on the storyboard file and choose Open As -> Source Code), I saw this:

<segue destination="UWX-rF-KOc" kind="replace" identifier="showStory" trigger="accessoryAction" splitViewControllerTargetIndex="1" id="Gly-La-KIe"/>

Note the trigger attribute. This suggests to me that the segue will be fired from the accessory action of the cell. You can even see this from the connections inspector of the cell:

Removing this and replacing the segue by dragging from the "selection" handle above instead of control-dragging from the cell gave me the correct segue.

I'm not sure what it is about this particular cell that makes control-dragging connect to the accessory action instead of the selection action, but there you go.

As stated above, you need to use didSelectRowAtIndexPath unless you configure a segue in your storyboard to a new UIViewController. This allows you to use the prepareForSegue function instead of the programmatic call of performSegueWithIdentifier.

Hope that helps clear it up!

My problem was the cell identifier.

So, in - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath I declared static NSString *cellIdentifier = @"cell"; and then in storyBoard I added this identifier in here:

And here you go!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!