如何在tableView中找到tapPath附件按钮的indexPath

Guf*_*ros 15 objective-c uitableview ios segue

UITableView有一个部分和几行简单.当用户单击单元附件按钮(与之连接时detailsSegue.我想知道它是什么单元格行.所以我可以从我的数组中选择正确的对象并将其分配给下一个视图中的变量.

我已经使用委托方法tableview:accessoryButtonTappedForRowWithIndexPath:并将indexPath值分配给我的私有属性myRow.比在prepareForSegue:sender:方法中我使用我的self.myRow.row值从数组中选择正确的对象.

我的问题是这两种方法似乎执行顺序错误.从NSLog我可以看到该prepareForSegue:sender:方法首先执行,我的委托方法正在改变self.myRow它之后的值.

因此,prepareForSegue:sender:方法总是将错误的对象传递给下一个视图(之前被点击的视图).

对不起,我的英国人.提前感谢您的帮助.

-(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;
    }
}
Run Code Online (Sandbox Code Playgroud)

rob*_*off 42

正如您所发现的那样,系统会在向您发送prepareForSegue:sender:消息之前向您发送tableview:accessoryButtonTappedForRowWithIndexPath:消息.

但是,当它向您发送prepareForSegue:sender:消息时,sender参数是UITableViewCell包含附件视图.您可以使用它来确定点击了哪一行的附件按钮:

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;
}
Run Code Online (Sandbox Code Playgroud)