IndexPath for segue from accessoryButton

倖福魔咒の 提交于 2019-12-22 04:21:39

问题


I am attempting to use a storyboard/segue to handle the transition between a UITableView with both standard transition as well as detail disclosure button. Having read a few different posts on here I have set up my project this way:

  1. Tie main segue between UITableViewCell and ViewController
  2. Tie secondary segue from parent ViewController to new ViewController
  3. Implement accessoryButtonTappedForRowWithIndexPath as follows:

    -(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath {
        [self performSegueWithIdentifier:@"DetailSegue" sender:self];
    }
    

This works great and my prepareForSegue: sender: gets called as expected. The trouble is, I need to know the indexPath for the element selected. The segue from the UITableViewCell retrieves the indexPath like this:

NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];

Unfortunately when I try to do that having called the accessoryButton, that returns null.

The original question I am basing some of this code off of is here: Detail Disclosure Button and Segues

Is there a method of the tableView which returns indexPath for accessoryButtons? Do I need to access the indexPath in some other manner?


回答1:


You don't have to override accessoryButtonTappedForRowWithIndexPath.

In prepareForSegue, when working with a detail disclosure button, instead of:

[self.tableView indexPathForSelectedRow]

use:

[self.tableView indexPathForCell:sender]

The sender is already the detail disclosure button's cell.




回答2:


The sender argument is, according to the documentation:

The object that you want to use to initiate the segue. This object is made available for informational purposes during the actual segue.

I don't see any reason why you can't use the index path as the sender instead of self, then access the index path in prepareForSegue:.

If that doesn't work, store the index path in an ivar and access that in prepareForSegue



来源:https://stackoverflow.com/questions/9339302/indexpath-for-segue-from-accessorybutton

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