ios - prepareForSegue 传递索引 0 的值...始终

标签 ios objective-c uitableview segue

我在 TableViewController 中显示了一组项目。它们在 TVC 中正确显示。下面的代码会继续,但它只会继续到我的 MKMapItem 数组的 indexPath 0,而不是被单击的单元格中的项目。

有没有想过我的错误在哪里?

@property (strong, nonatomic) NSArray *mapItems;

每个单元格都有一个“添加 POI”UIButton,它会触发使用 Interface Builder 创建的名为“addPOISegue”的转场。

Here's the Cell

Here's the Segue Identifier

这是“添加 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;
    }

}

关于ios - prepareForSegue 传递索引 0 的值...始终,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28968942/

相关文章:

ios - WebRTC ios设置

ios - 如何将 SDWebImage 转换为 UIImage?

ios - 滑动手势的多个 Action

objective-c - 为什么我总是看到双重属性(property)声明?

ios - 让Cell根据TextInput出现和消失

ios - 如何在swift框架中链接JRE.framework

objective-c - 在核心数据项目中加载 View Controller 时出现问题

iphone - 使用 scrollEnabled=NO 在 TableView 上滚动取消突出显示先前选择的行

iphone - 如何在仍然使用重新排序的同时摆脱 reorderControl?

ios - Google Maps iOS SDK 不会在 tableViewCell 中更新