tôi ở đây TableViewController
中显示了一组项目。它们在 TVC 中正确显示。下面的代码会继续,但它只会继续到我的 MKMapItem
数组的 indexPath 0
,而不是被单击的单元格中的项目。
有没有想过我的错误在哪里?
@property (strong, nonatomic) NSArray *mapItems;
每个单元格都有一个“添加 POI”UIButton
,它会触发使用 Interface Builder 创建的名为“addPOISegue”的转场。
这是“添加 POI”按钮的 IBAction:
- (IBAction)addPOIButtonClicked:(UIButton *)sender {
NSLog(@"Add POI button clicked");
[self performSegueWithIdentifier:@"addPOISegue" sender:sender];
}
这是`prepareForSegue
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(UIButton*)sender {
// NSLog(@"The sender is %@", sender);
if ([[segue identifier] isEqualToString:@"addPOISegue"]) {
AddPOIViewController *destinationVC = segue.destinationViewController;
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:sender.center];
NSLog(@"NSIndexPath *indexPath's index is %@", indexPath);
MKMapItem *item = _mapItems[indexPath.row];
// NSLog(@"ResultsTVC item is %@", item);
destinationVC.item = item;
}
}
indexPath 一直设置为 0。我怀疑这是因为我有一个按钮触发单元格内的 segue,但我不知道如何解决这个问题。
您应该删除按钮的操作方法,并将转场直接从按钮连接到下一个 Controller 。在prepareForSegue中,可以将按钮的原点,转换为tableview的坐标系后,传递给indexPathForRowAtPoint:方法来获取按钮所在单元格的indexPath,
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(UIButton*)sender {
if ([[segue identifier] isEqualToString:@"addPOISegue"]) {
AddPOIViewController *destinationVC = segue.destinationViewController;
CGPoint p = [sender convertPoint:CGPointZero toView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:p];
NSLog(@"NSIndexPath *indexPath's index is %@", indexPath);
MKMapItem *item = _mapItems[indexPath.row];
// NSLog(@"ResultsTVC item is %@", item);
destinationVC.item = item;
}
}
Tôi là một lập trình viên xuất sắc, rất giỏi!