I\'ve simple UITableView
with one section and few rows. When user clicks cell accessory button (which is connected with detailsSegue
. I want to kno
// I tell you a simple and a better and an easy option :-
// Solution :- You can assign tag to button of accessory view in this method of tableView
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// set accessory view
UIButton *oValidHomeAccessryBtn = [UIButton buttonWithType:UIButtonTypeCustom];
// set target of accessory view button
[oValidHomeAccessryBtn addTarget:self action:@selector(AccessoryAction:) forControlEvents:UIControlEventTouchUpInside];
// set tag of accessory view button to the row of table
oValidHomeAccessryBtn.tag =indexPath.row;
// set button to accessory view
cell.accessoryView = oValidHomeAccessryBtn ;
}
// Make the method you passed in selector and get tag value and you would get indexPath of accessory view clicked
- (void)AccessoryAction:(id)sender
{
NSLog(@"%d",[sender tag]);
}
This is the simplest way to get the indexPat of row in which you clicked the accessory view.
The sender will be the accessory button, right? In that case you should be able to find its containing cell by going up through super views and then get the index path of that cell. I’ve used a method like this before to do the first part:
+ (UITableViewCell *)findParentCellOfView:(UIView *)view {
if (view == nil || [view isKindOfClass:[UITableViewCell class]]) {
return (UITableViewCell *)view;
}
return [self findParentCellOfView:[view superview]];
}
What you need to do is call the segue
programmatically from the delegate method. Assuming you're using storyboards, you'll need to unlink the segue
in the storyboard and in your delegate method use:
[self performSegueWithIdentifier: @"IdentifierNameHere" sender: self];
As you have discovered, the system sends you the prepareForSegue:sender:
message before it sends you the tableview:accessoryButtonTappedForRowWithIndexPath:
message.
But, when it sends you the prepareForSegue:sender:
message, the sender
argument is the UITableViewCell
containing the accessory view. You can use that to determine which row's accessory button was tapped:
else if ([segue.identifier isEqualToString:@"detailsSegue"]) {
NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
Annotation *annotation = [self.viewsList objectAtIndex:indexPath.row];
DetailsViewController *dvc = segue.destinationViewController;
dvc.wikiKey = annotation.title;
}