我在TableViewController
中显示了一系列项目。它们在TVC中正确显示。下面的代码是segues,但它只是indexPath 0
的{{1}}个MKMapItem
,而不是单击的单元格中的项目。
有关我的错误在哪里的任何想法?
@property (strong, nonatomic) NSArray *mapItems;
每个单元格都有一个“添加POI”UIButton
,它触发了一个名为“addPOISegue”的Interface Builder创建的segue。
以下是“添加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的按钮,但我很难过如何解决这个问题。
答案 0 :(得分:1)
您应该删除按钮的操作方法,并将segue直接从按钮连接到下一个控制器。在prepareForSegue中,您可以将按钮的原点转换为将表视图的坐标系转换为indexPathForForCowAtPoint:方法,以获取该按钮所包含的单元格的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;
}
}
答案 1 :(得分:0)
@rdelmar想通了。由于我的单元格中包含CGPoint
并且在UIButton
方法中引用,我需要prepareForSegue
。
此代码使其正常运行:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(UIButton*)sender {
// NSLog(@"The sender is %@", sender);
if ([[segue identifier] isEqualToString:@"addPOISegue"]) {
AddPOIViewController *destinationVC = segue.destinationViewController;
CGPoint point = [sender convertPoint:CGPointZero toView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:point];
NSLog(@"NSIndexPath *indexPath's index is %@", indexPath);
MKMapItem *item = _mapItems[indexPath.row];
NSLog(@"ResultsTVC item is %@", item);
destinationVC.item = item;
}
}