gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 02:13:54 28 4
gpt4 key购买 nike

我在 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com