【问题标题】:How to find indexPath for tapped accessory button in tableView如何在 tableView 中找到点击的附件按钮的 indexPath
【发布时间】:2012-11-21 07:37:29
【问题描述】:

我有一个简单的UITableView,只有一个部分和几行。当用户单击单元格附件按钮(与detailsSegue 连接时。我想知道它是哪个单元格行。所以我可以从我的数组中选择正确的对象并将其分配给下一个视图中的变量。

我使用了委托方法 tableview:accessoryButtonTappedForRowWithIndexPath: 并将 indexPath 值分配给我的私有属性 myRow。比在prepareForSegue:sender: 方法中,我使用我的self.myRow.row 值从数组中选择正确的对象。

我的问题是这两种方法似乎以错误的顺序执行。从 NSLog 我可以看到 prepareForSegue:sender: 方法首先执行,我的委托方法在它之后更改了 self.myRow 的值。

所以prepareForSegue:sender: 方法总是将错误的对象传递给下一个视图(之前点击的那个)。

对不起我的英语伙计们。提前感谢您的帮助。

-(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath {
    self.myRow = indexPath;
    NSLog(@"tapped button at row: %i",self.myRow.row); 
}

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if([segue.identifier isEqualToString:@"addSegue"]) {
        AddViewController *avc = segue.destinationViewController;
        avc.delegate = self;
    }
    else if ([segue.identifier isEqualToString:@"detailsSegue"]) {
        NSLog(@"Segue row: %i",self.myRow.row);
        Annotation *annotation = [self.viewsList objectAtIndex:self.myRow.row];
        NSLog(@"Segue annotation object: %@",annotation.title);
        DetailsViewController *dvc = segue.destinationViewController;
        dvc.wikiKey = annotation.title;
    }
}

【问题讨论】:

    标签: objective-c ios uitableview segue


    【解决方案1】:

    正如您所发现的,系统在向您发送tableview:accessoryButtonTappedForRowWithIndexPath: 消息之前向您发送prepareForSegue:sender: 消息。

    但是,当它向您发送prepareForSegue:sender: 消息时,sender 参数是包含附件视图的UITableViewCell。您可以使用它来确定点击了哪一行的辅助按钮:

    else if ([segue.identifier isEqualToString:@"detailsSegue"]) {
        NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
        Annotation *annotation = [self.viewsList objectAtIndex:indexPath.row];
        DetailsViewController *dvc = segue.destinationViewController;
        dvc.wikiKey = annotation.title;
    }
    

    【讨论】:

      【解决方案2】:

      发送者将是附件按钮,对吗?在这种情况下,您应该能够通过超级视图向上找到它的包含单元格,然后获取该单元格的索引路径。我以前用过这样的方法来做第一部分:

      + (UITableViewCell *)findParentCellOfView:(UIView *)view {
        if (view == nil || [view isKindOfClass:[UITableViewCell class]]) {
          return (UITableViewCell *)view;
        }
        return [self findParentCellOfView:[view superview]];
      }
      

      【讨论】:

      • 发件人实际上是单元格,而不是附件按钮。
      【解决方案3】:

      // 我告诉你一个简单、更好、更简单的选择:-

      // 解决方案:- 可以在tableView的这个方法中给附属视图的按钮分配标签

      -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
      {
          // set accessory view
          UIButton *oValidHomeAccessryBtn = [UIButton buttonWithType:UIButtonTypeCustom];
      
          // set target of accessory view button
          [oValidHomeAccessryBtn addTarget:self action:@selector(AccessoryAction:) forControlEvents:UIControlEventTouchUpInside];
      
          // set tag of accessory view button to the row of table
          oValidHomeAccessryBtn.tag =indexPath.row;
      
          // set button to accessory view
          cell.accessoryView = oValidHomeAccessryBtn ;
      }
      

      // 将你传入的方法设置为选择器并获取标签值,你会得到点击的附件视图的indexPath

      - (void)AccessoryAction:(id)sender
      {
          NSLog(@"%d",[sender tag]);
      }
      

      这是获取您单击附件视图所在行的 indexPat 的最简单方法。

      【讨论】:

        【解决方案4】:

        您需要做的是从委托方法中以编程方式调用segue。假设您正在使用情节提要,您需要在情节提要和委托方法使用中取消segue 的链接:

        [self performSegueWithIdentifier: @"IdentifierNameHere" sender: self];
        

        【讨论】:

          猜你喜欢
          • 2014-07-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-05-13
          • 1970-01-01
          • 2020-12-07
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多