我有一个简单的 UITableView
,只有一个部分和几行。当用户单击单元格附件按钮(与 detailsSegue
连接时。我想知道它是哪个单元格行。这样我就可以从我的数组中选择正确的对象并将其分配给下一个 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/