UITableViewCell子视图的NSIndexPath?

Kel*_*ler 5 objective-c uibutton uitableview ios

有没有得到UITableViewCell的contentView子视图的NSIndexPath?

在我的例子中,我有一个表视图,其中每行被分成3个按钮.我可以使用按钮标记来跟踪它是哪个列,但我还需要知道表视图的哪一行是(当用户点击按钮时)的子视图.

因为按钮会阻塞整个实际的表视图行,所以从不调用didSelectRowAtIndexPath(这是预期的),所以我不能这样做.

我曾尝试过[thisButton superview],但这似乎不起作用.有什么想法吗?

gsc*_*ler 19

我假设按钮正在将其UIControlEventTouchUpInside事件消息发送给您的视图控制器?然后你可以这样做:

- (void)buttonPressed:(UIButton *)button
{
    CGPoint location = [button.superview convertPoint:button.center toView:self.tableView];
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location];
    [self doSomethingWithRowAtIndexPath:indexPath];
}
Run Code Online (Sandbox Code Playgroud)

  • 这个答案实际上是我想要的更多,更优雅. (2认同)