【问题标题】:How to get indexPath over touched button?如何在触摸按钮上获取 indexPath?
【发布时间】:2012-10-22 15:24:25
【问题描述】:

我有动态 tableView 和一个问题。每个 tableCell 中有两个按钮。 我试图让 indexPath.row 按钮被触摸但没有成功。

我的代码:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{

    NSIndexPath *indexPath = [self.homeTableView indexPathForSelectedRow];      

    NSLog(@"%d", indexPath.row);


}

我怎样才能获得触动按钮的 indexPath.row(Segue 连接在这两个按钮上 -> 两个连接)?

【问题讨论】:

  • 我得到了 nil -> NSLog(@"%d", indexPath.row);
  • %d 说明符不应显示 nil。也许您选择了索引为 0 的第一行。
  • 当我选择第二行或第三行时总是为零。
  • 我没想到这会奏效。当您在表格视图中有一个控件的IBOutlet 时,按下按钮时不会选择UITableViewCell。您需要一些从按钮返回到UITableViewCell 的链接。如果您刚刚将按钮设置为单元格本身的子视图,则可以通过superview 检索单元格,然后通过UITableView 方法indexPathForCell 检索 indexPath。

标签: ios xcode uitableview


【解决方案1】:

它不起作用,因为您单击按钮时没有选择行。

使用两个按钮,我会将您的 segues 与按钮断开连接,并在 IB 中执行 2 个手动 segues,然后添加类似的代码来处理它们:

-(void)button1Pressed:(id)sender {
    UITableViewCell *clickedCell = (UITableViewCell *)[[sender superview] superview];
    NSIndexPath *clickedButtonIndexPath = [self.homeTableView indexPathForCell:clickedCell];
    ...
    [self performSegueWithIdentifier:@"YourManualSegueIdentifier1" sender:self];
}

编辑第二个选项:

在您的 MyTableViewCell.h 中:

@interface MyTableViewCell : UITableViewCell
    ...    
    @property (strong, nonatomic) NSIndexPath *cellIndexPath;
    ...
@end

在您的 MyTableViewCell.m 中:

-(void)button1Pressed:(id)sender {
    ...
    NSInteger row = cellIndexPath.row;
    ...
}

在您的 MyTableViewController.m 中:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MyTableViewCell *cell = (MyTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[MyTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];      
    }
    // remember index here!
    [cell setCellIndexPath:indexPath];
    ....
    return cell;
}

【讨论】:

  • 但我得到了 uitableviewcell.h 中按钮的 IBaction
  • 因此,如果您有自定义 UITableViewCell 类,您可以使用 indexPath 属性对其进行扩展,并在 cellForTableView: 中检索单元格时对其进行更新,但前提是您不打算在中间插入行或删除它们。
  • 对不起,我不明白你的意思。你能详细解释一下吗?附言我不打算在中间插入或删除行。
  • 如果你继承UITableViewCell,你可以在那里添加额外的属性,例如cellIndexPath。在您返回“cellForRowAtIndexPath:”内的单元格的 viewController 中,您可以将 cellIndexPath 设置为 indexPath 参数,然后在您的 button1Pressed 中,您可以从 self.cellIndexPath 获取它。
  • 您想获取分配了按钮的单元格的索引。此方法应该是您的自定义 UITableViewCell 按钮的 IBAction。因此,如果您按下按钮,您将获得该功能。从那里您可以致电[[[self superview] superview] performSegueWithIdentifier:@"YourSequeBtn1" sender:self]]。在prepareForSegue 中,您可以从 sender 参数中读取单元格,从而也可以获取行索引。无论如何,您的按钮解决方案似乎不那么复杂。您始终可以将按钮子类化并将 indexPath 保留在其中而不是使用标记,这样您也将获得部分
【解决方案2】:

Apple 在 iOS 7 中更改 UITableViewCell 层次结构

使用 iOS 6.1 SDK

<UITableViewCell>
   | <UITableViewCellContentView>
   |    | <UILabel>

使用 iOS 7 SDK

<UITableViewCell>
   | <UITableViewCellScrollView>
   |    | <UITableViewCellContentView>
   |    |    | <UILabel>

因此,如果您想在按钮索引处获取索引路径,请使用以下代码 使用 iOS 6 或更高版本的 SDK

UITableViewCell *clickedCell = (UITableViewCell *)[[sender superview] superview];
    NSIndexPath *clickedButtonPath = [book_table indexPathForCell:clickedCell];
    NSLog(@"index=%@",clickedButtonPath);

使用 iOS 7 或 7+ SDK

 UITableViewCell *clickedCell = (UITableViewCell *)[[[sender superview] superview]superview];
    NSIndexPath *clickedButtonPath = [book_table indexPathForCell:clickedCell];
    NSLog(@"index=%ld",(long)clickedButtonPath.item);

【讨论】:

    【解决方案3】:

    我没有使用故事板,但是一旦我在基于简单 viewconroller 的项目中使用它,我认为这可能会对您有所帮助。

     - (IBAction)goToNew:(id)sender 
    {
       UIButton *btn = (UIButton*) sender;
       UITableViewCell *cell = (UITableViewCell*) [btn superview];
       NSIndexPath *indexPath = [tableView indexPathForCell:cell]; 
       int row = indexPath.row;
    }
    

    【讨论】:

    • +1 同意。您需要从按钮映射回UITableViewCell,然后再映射到indexPath。具体细节可能会根据按钮和单元格之间的关系而有所不同(例如,如果按钮位于内容视图中,您将需要通过一个额外的级别,但希望 CroiOS 能够理解)。
    • yes @Rob 如果它在内容视图中,那么 UITableViewCell cell = (UITableViewCell) btn.superview.superview;应该在那里。
    • 但我得到了 uitableviewcell.h 中按钮的 IBaction
    • 然后将单元格的目标提供给您的表格视图所在的视图控制器。比如在你的控制器中写 cell.target = self 并在你的控制器中写一个 - (IBAction)goToNew:(id)sender 方法。
    • 在 iOS8 中..this 将始终为 0。请参阅此....stackoverflow.com/questions/23784630/…
    【解决方案4】:

    也许触摸发生在 UIButton 而不是 UITableView / UITableViewCell。

    检查:

    NSLog(@"%@",[sender class]);
    

    【讨论】:

    • 是的,很抱歉,但我不知道如何解释。触摸发生在 UIButton 上。我可以得到 indexPath.row 吗?
    • 如果按钮是UITableViewCell的子视图,那么你可以使用[button superview]方法访问单元格,然后访问它的indexPath.row属性。
    • 但我得到了 uitableviewcell.h 中按钮的 IBaction
    【解决方案5】:

    解决了 button.tag

    在“cellForRowAtIndexPath”中,我设置 button.tag = indexPath.row,然后在“- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {"

    UIButton *btn = (UIButton*) sender;
    NSLog(@"%d", btn.tag);
    

    【讨论】:

    • 很好的解决方案,但前提是您的表格中有一个部分。
    • 并且您不会在不重新加载整个表格的情况下删除单元格。
    【解决方案6】:

    给按钮tag = indexpath.row+100;

    然后得到..

    -(IBAction)ShowListView:(id)sender
    {
    
        UIButton *btn = (UIButton *)sender;
    
        int Iindex = btn.tag-100;
    
        NSLog(@"button title is %@",btn.titleLabel.text);
        NSLog(@"button id is %d",Iindex);
    
        NSLog(@"array category is....%@",arrayCategory);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-30
      • 2013-09-30
      • 1970-01-01
      • 2012-04-28
      • 1970-01-01
      • 2021-02-18
      相关资源
      最近更新 更多