tableview的点击事件获取行号 indexpath

 UITableView 有一个很关键的方法 indexPathForRowAtPoint,可以根据触摸发生的位置,返回触摸发生在哪一个 cell 的indexPath。 而且通过 event 对象,我们也可以获得每个触摸在视图中的位置:

// 检查用户点击按钮时的位置,并转发事件到对应的accessorytapped事件
- (void)btnClicked:(id)senderevent:(id)event
{
NSSet *touches =[event allTouches];
UITouch *touch =[touches anyObject];
CGPointcurrentTouchPosition = [touch locationInView:self.tableView];
NSIndexPath *indexPath= [self.tableView indexPathForRowAtPoint:currentTouchPosition];
if (indexPath!= nil)
{
[self tableView: self.tableView accessoryButtonTappedForRowWithIndexPath:indexPath];
}
}

这样,UITableView的accessoryButtonTappedForRowWithIndexPath方法会被触发,并且获得一个indexPath 参数。通过这个indexPath 参数,我们可以区分到底是众多按钮中的哪一个附件按钮发生了触摸事件:

-(void)tableView:(UITableView*)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath*)indexPath{
int* idx= indexPath.row;
//在这里加入自己的逻辑
⋯⋯
}
原文地址:https://www.cnblogs.com/sozui/p/4360078.html