Segue to current view UItableView

别来无恙 提交于 2019-12-06 12:07:41
Rob

If you're using dynamic cell prototypes, you obviously can do a segue from the table view cell to the controller without any problem.

When you make your segue, you end up with:

But let's imagine for a second that there's some reason that doesn't work for you, e.g. you could not have a segue from the cell (for example, you need to invoke segue from didSelectRowAtIndexPath, not upon selecting the cell). In this case, you couldn't use that previous technique.

There are a couple of options in this case:

  1. As pointed out by Chris, if only supporting iOS 6 and above, use the above technique, but (a) make sure your didSelectRowAtIndexPath uses self for the sender and then have shouldPerformSegueWithIdentifier only allow the segue if the sender == self (thus when the sender is the table view cell, it will be canceled);

  2. Perhaps even easier, just don't define a segue at all and have your didSelectRowAtIndexPath manually instantiateViewControllerWithIdentifier and then push/present that view controller as appropriate; or

  3. You can use the following, kludgy work-around: In short, add a button to your view controller (drag it down to the bar at the bottom), where it won't show up on the view, but you can use its segues. So first, drag the rounded button to the controller's bar at the bottom of the scene:

    Now you can make a segue from that button to your view controller:

    And thus, you end up with the self-referential segue again:

    You must give that segue an identifier, so you can invoke it programmatically via performSegueWithIdentifier, but it works. Not the most elegant of solutions, but I think it's better than having an extra scene on your storyboard.

None of these are ideal, but you have lots of options.

This process worked for me to do the exact same thing...

Set up your tableview in storyboard as usual. DO NOT give your prototype cell a Cell Identifier. Set up your segue as you did above, by dragging from the cell to the controller.

In your viewDidLoad register the cell class

//register the cell identifier as we are not loading form a storybard
[_aTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"CellID"];

In your cellForRowAtIndexPath set up your cell

static NSString *CellIdentifier = @"CellID";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

Now you can use didSelectRowAtIndexPath and call performSegueWithIdentifier. the prepareForSegue will only be called once.

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