如何从单元格中的按钮操作获取tableview中的indexPath?

Syl*_*sot 6 xcode uitableview nsindexpath

我有一个包含两个动作的tableview,第一个我使用委托didSelectRowAtIndexPath,第二个我想在我的单元格上的按钮上使用动作来做其他事情.我的问题是我没有成功获得索引路径?这样做的最佳做法是什么?

Emp*_*ack 9

如果您已将按钮添加到单元格中,

[cell.contentView addSubview:button];
Run Code Online (Sandbox Code Playgroud)

那么,你可以获得索引路径,

- (void)onButtonTapped:(UIButton *)button {

    UITableViewCell *cell = (UITableViewCell *)button.superview.superview;
    NSIndexPath *indexPath = [tableView indexPathForCell:cell];
    // Go ahead
}
Run Code Online (Sandbox Code Playgroud)