表格中的按钮索引,iPhone



我在UITableView的每一行都有UIButtons。如何检测按钮索引(就像我们可以通过didSelectRowIndexAtIndexPath方法检测表中的行索引一样)?

例如:我有一个自定义UITableViewCells UITableView.在 10 行中,有 6 行有 UIButtons .

点击按钮时,我将用户带到下一个视图(详细信息视图)。但是我无法获得UIButton(行)索引。由于我不是通过didSelectRowIndexAtIndexPath方法在按钮单击时带用户,因此我无法获取行索引。

我怎样才能得到它?

建议。谢谢

在以下位置执行此操作:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  //other code
  // your button reference here
  // if custom cell then use cell.yourbutton
  [button addTarget:self action:@selector(buttonPressedAction:) forControlEvents:UIControlEventTouchUpInside];
  [button setTag:indexPath.row];
  //other code
}

现在按钮选择器将如下所示:

- (void)buttonPressedAction:(id)sender
{
   UIButton *button = (UIButton *)sender;
   int row = button.tag;
}

最新更新