Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IndexPath for segue from accessoryButton

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?

like image 588
valdarin Avatar asked Feb 18 '12 06:02

valdarin


2 Answers

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.

like image 88
Adam Avatar answered Nov 06 '22 04:11

Adam


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

like image 39
jrturton Avatar answered Nov 06 '22 02:11

jrturton