Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
536 views
in Technique[技术] by (71.8m points)

objective c - How to find indexPath for tapped accessory button in tableView

I've simple UITableView with one section and few rows. When user clicks cell accessory button (which is connected with detailsSegue. I want to know what cell row was it. So than I can select right object from my array and assign it to the variable from the next view.

I have used delegate method tableview:accessoryButtonTappedForRowWithIndexPath: and assigned indexPath value to my private property myRow. Than in prepareForSegue:sender: method I used my self.myRow.row value to select right object from array.

My problem is that these two methods seems to execute in wrong order. From the NSLog I can see that prepareForSegue:sender: method is executed first and my delegate method is changing value of self.myRow after it.

So prepareForSegue:sender: method is always passing wrong object to the next view (the one that was tapped previously).

Sorry for my english guys. Thanks for help in advance.

-(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath {
    self.myRow = indexPath;
    NSLog(@"tapped button at row: %i",self.myRow.row); 
}

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if([segue.identifier isEqualToString:@"addSegue"]) {
        AddViewController *avc = segue.destinationViewController;
        avc.delegate = self;
    }
    else if ([segue.identifier isEqualToString:@"detailsSegue"]) {
        NSLog(@"Segue row: %i",self.myRow.row);
        Annotation *annotation = [self.viewsList objectAtIndex:self.myRow.row];
        NSLog(@"Segue annotation object: %@",annotation.title);
        DetailsViewController *dvc = segue.destinationViewController;
        dvc.wikiKey = annotation.title;
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...