objective-c - 如何在 tableView 中找到点击的附件按钮的 indexPath

标签 objective-c ios uitableview segue

我有一个简单的 UITableView,只有一个部分和几行。当用户单击单元格附件按钮(与 detailsS​​egue 连接时。我想知道它是哪个单元格行。这样我就可以从我的数组中选择正确的对象并将其分配给下一个 View 中的变量.

我使用委托(delegate)方法 tableview:accessoryButtonTappedForRowWithIndexPath: 并将 indexPath 值分配给我的私有(private)属性 myRow。在 prepareForSegue:sender: 方法中,我使用了 self.myRow.row 值从数组中选择正确的对象。

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

所以 prepareForSegue:sender: 方法总是将错误的对象传递给下一个 View (之前被点击的那个)。

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

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

最佳答案

正如您所发现的,系统向您发送 prepareForSegue:sender: 消息 之前 它向您发送 tableview:accessoryButtonTappedForRowWithIndexPath: 消息。

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

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

关于objective-c - 如何在 tableView 中找到点击的附件按钮的 indexPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13690008/

相关文章:

ios - 如何在 iPhone 中更改 UItableview 的高度和宽度

iphone - 使用 iPhone CoreLocation 框架时哪个线程调用委托(delegate)?

ios - 如何在 Objective-C 、iOS 中将 NSMutableArray 数组分成两个单独的数组或两个可变数组

iphone - UITableView 中的 UIWebView 变慢

objective-c - Objective-C : How do I add custom table view cells to a subview of a UIViewController

ios - Objective-c 属性未在 ARC 下发布

ios - NSTimeInterval 并查看日期是否超过一个小时

ios - [iOS]捕获用户位置: clarification

ios - 如何使用 UICollectionViewCompositionalLayout 创建具有相等行高的网格布局

ios - Swift 性能和 CustomStringConvertible 协议(protocol)