我有一个简单的UITableView,其中有一部分,只有几行。当用户单击单元格附件按钮(它与detailsSegue连接时。我想知道它在哪个单元格行中。因此,我可以从数组中选择合适的对象,然后从下一个 View 中将其分配给变量。

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

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

因此,prepareForSegue:sender:方法始终将错误的对象传递给下一个 View (先前单击的 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参数是包含附件 View 的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;
}

10-04 23:05
查看更多