Ios uitableviewcell中的uibutton给我的indexpath值比实际的indexpath值少1

Ios uitableviewcell中的uibutton给我的indexpath值比实际的indexpath值少1,ios,objective-c,uitableview,Ios,Objective C,Uitableview,当用户按下单元格的子视图按钮时,我试图查找UITableViewCell的行。使用下面的代码,我可靠地得到了一个比实际的indepath少的值。有人知道为什么/如果这是在iOS7中使用子视图(在本例中是ui按钮)查找单元格的indexpath的好方法吗 根据我的建议,请执行以下操作: 将按钮标记设置为indexPath.row,如下所示: cell.button.tag = indexPath.row; - (void)buttonClicked:(id)sender {

当用户按下单元格的
子视图
按钮时,我试图查找
UITableViewCell
的行。使用下面的代码,我可靠地得到了一个比实际的
indepath
少的值。有人知道为什么/如果这是在iOS7中使用
子视图
(在本例中是
ui按钮
)查找单元格的
indexpath
的好方法吗


根据我的建议,请执行以下操作:

将按钮标记设置为indexPath.row,如下所示:

 cell.button.tag = indexPath.row;
- (void)buttonClicked:(id)sender {    
       UIButton *btnTemp=(UIButton *)sender;
       NSLog(@"%d",btnTemp.tag);
}
现在在该按钮上添加选择器,如以下示例代码所示:

[cell.button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
现在实现选择器并查找所选行的索引,如下所示:

 cell.button.tag = indexPath.row;
- (void)buttonClicked:(id)sender {    
       UIButton *btnTemp=(UIButton *)sender;
       NSLog(@"%d",btnTemp.tag);
}

因此,在btnTemp.tag中,您可以找到所选单元格的ccorrect索引

设置按钮标签并获取单击按钮的索引路径

[yourButton setTag:indexPath.row];
[yourButton addTarget:self action:@selector(yourButtonClick:) forControlEvents:UIControlEventTouchUpInside];

-(void)yourButtonClick:(id)sender
{
    CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:yourTableView];
    NSIndexPath *deliverIndPath = [yourTableView indexPathForRowAtPoint:buttonPosition];
    NSLog(@"deliverIndPath : %@",deliverIndPath);
}

CGPointZero
更改为
CGPointMake(5,5)
,您应该会得到更好的结果。设置标记与获取indexpath有什么关系?如果您在tableview中创建多个按钮,则将indexpath.row设置为按钮标记,同时单击按钮(id)通过发送程序,您将获得该单击按钮行的indexpath。在DeliveryIndPath中是indexpath。