如何知道UITableView中Tableview单元格按钮上的节号?

Ale*_*hel 0 iphone uitableview ios

我有一个UITableView,我已经为其创建了一个自定义的UITableViewCell.tableview中的每一行都有一个按钮.我想知道单击按钮时的节号,以便我知道单击了哪个部分按钮.我已经尝试过在堆栈上找到的一些东西,但没有任何工作.

UIButton *b = sender; 
NSIndexPath *path = [NSIndexPath indexPathForRow:b.tag inSection:0]; 
NSLog(@"Row %d - Section : %d", path.row, path.section);
Run Code Online (Sandbox Code Playgroud)

Jab*_*air 5

不知道你尝试了什么,但我可能会做这样的事情.在这里从内存中做一些伪代码.

- (void)buttonClicked:(id)sender {
    CGPoint buttonOrigin = [sender frame].origin;
    // this converts the coordinate system of the origin from the button's superview to the table view's coordinate system.
    CGPoint originInTableView = [self.tableView convertPoint:buttonOrigin fromView:[sender superview];

    // gets the row corresponding to the converted point
    NSIndexPath rowIndexPath = [self.tableView indexPathForRowAtPoint:originInTableView];

    NSInteger section = [rowIndexPath section];

}
Run Code Online (Sandbox Code Playgroud)

如果我清楚地思考,这可以让您在按钮不直接位于UITableView单元格内时具有灵活性.比方说,如果你嵌套在一些中间视图中.

可悲的是,似乎没有iOS等效的NSTableView rowForView: