prepareForSegue 传递索引 0 的值...总是 [英] prepareForSegue passes value for Index 0...ALWAYS

查看:22
本文介绍了prepareForSegue 传递索引 0 的值...总是的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an array of items displayed in a TableViewController. They display properly in the TVC. The code below segues, but it only segues to indexPath 0 of my array of MKMapItems, not the item from the cell that was clicked.

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

Any thoughts on where my mistake is?

@property (strong, nonatomic) NSArray *mapItems;

每个单元格都有一个添加 POI"UIButton,它会触发使用 Interface Builder 创建的名为addPOISegue"的 segue.

Each cell has a "Add POI" UIButton which that triggers a segue created with Interface Builder called "addPOISegue".

这是添加兴趣点"按钮的 IBAction:

Here's the IBAction for the "Add POI" button:

- (IBAction)addPOIButtonClicked:(UIButton *)sender {
    NSLog(@"Add POI button clicked");
    [self performSegueWithIdentifier:@"addPOISegue" sender:sender];
}

这是`prepareForSegue

Here's the `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,但我不知道如何解决这个问题.

The indexPath keeps getting set to 0. I suspect this is because I've got a button triggering the segue within the cell, but I'm stumped as to how to resolve this.

推荐答案

你应该删除按钮的 action 方法,并将 segue 直接从按钮连接到下一个控制器.在prepareForSegue中,你可以通过按钮的原点,将其转换为表格视图的坐标系后传递给indexPathForRowAtPoint:方法来获取按钮所在单元格的indexPath,

You should delete the button's action method, and connect the segue directly from the button to the next controller. In prepareForSegue, you can pass the button's origin, after converting it to the table view's coordinate system to the indexPathForRowAtPoint: method to get the indexPath of the cell that button is contained in,

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

}

这篇关于prepareForSegue 传递索引 0 的值...总是的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆