Objective c UITableViewCell子视图的NSIndexPath?

Objective c UITableViewCell子视图的NSIndexPath?,objective-c,ios,uitableview,uibutton,Objective C,Ios,Uitableview,Uibutton,是否有任何was可以获取UITableViewCell的contentView子视图的NSIndexPath 在我的例子中,我有一个表视图,其中每一行被分成3个按钮。我可以使用button标记跟踪它是哪一列,但我还需要知道它是表视图的哪一行的子视图(当用户点击按钮时) 由于该按钮阻止了整个实际的表视图行,因此从未调用过didSelectRowAtIndexPath(这是预期的),因此我无法以这种方式获得它 我尝试过[这个按钮超级视图],但似乎不起作用。有什么想法吗?一个可能的选择是,当为索引路径

是否有任何was可以获取UITableViewCell的contentView子视图的NSIndexPath

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

由于该按钮阻止了整个实际的表视图行,因此从未调用过didSelectRowAtIndexPath(这是预期的),因此我无法以这种方式获得它


我尝试过[这个按钮超级视图],但似乎不起作用。有什么想法吗?

一个可能的选择是,当为索引路径创建单元格时,您可以将标记指定为
3*行号+
。然后将标记除以3以获得行和%3以获得按钮。

我假设按钮正在将其
UIControlEventTouchUpInside
事件消息发送到您的视图控制器?然后你可以做一些类似的事情:

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

您不需要调用button.superview。可以直接从树中的任何子视图转到表视图

目标-C 敏捷的
这个答案实际上更符合我的要求,也更优雅。如果你选择这样做的话,它对插入/删除/重新排列行也免疫。嗯。。我的第三列按钮似乎不起作用。它正确地转换了触摸位置,但在indexPathForRowAtPoint上,第三列中的每个按钮都返回0(但前两列也适用)。这可能与景观有关吗?对不起,我从错误的坐标空间转换。因为我使用的是按钮
center
,所以我应该从它的
superview
坐标空间进行转换。我已经更新了代码片段。
- (NSIndexPath *)indexPathForCellContainingView:(UIView *)view inTableView:(UITableView *)tableView {
    CGPoint viewCenterRelativeToTableview = [tableView convertPoint:CGPointMake(CGRectGetMidX(view.bounds), CGRectGetMidY(view.bounds)) fromView:view];
    NSIndexPath *cellIndexPath = [tableView indexPathForRowAtPoint:viewCenterRelativeToTableview];
    return cellIndexPath
}
func indexPathForCellContainingView(view: UIView, inTableView tableView:UITableView) -> NSIndexPath? {
    let viewCenterRelativeToTableview = tableView.convertPoint(CGPointMake(CGRectGetMidX(view.bounds), CGRectGetMidY(view.bounds)), fromView:view)
    return tableView.indexPathForRowAtPoint(viewCenterRelativeToTableview)
}