[iOS]获取自定义button所在的行数(TableViewCell)

来源:互联网 发布:floyd算法输出路径 编辑:程序博客网 时间:2024/05/22 14:07

在自定义的 cell 上面放了一个 button, 除了通过 cell.btn.tag 值获取cell的行数之外,另一种方便可行的方式。
[cell.button addTarget:self action:@selector(cellButtonClicked:event:) forControlEvents:UIControlEventTouchUpInside];// 点击事件- (void)cellButtonClicked:(id)sender event:(id)event { NSSet * touches =[event allTouches]; UITouch * touch =[touches anyObject]; CGPoint currentTouchPosition = [touch locationInView:_tableView]; NSIndexPath * indexPath =[_tableView indexPathForRowAtPoint:currentTouchPosition]; if(indexPath!= nil) { // 代码区 }}
原文链接:http://www.jianshu.com/p/daeb20bc486b
iOS中如何通过点击UITableViewCell中的Button来获取当前Cell的indexPath
- (void) btnOnClickTouched:(UIButton *)aButton
{
myTableViewCell* myCell = (myTableViewCell *)[aButton superView]; //表示Button添加在了Cell中。
//如果将Button添加在myCell.contentView 中,
//myCell = (myTableViewCell *)[[aButton superView] superView];

indexPath cellPath = [self.tableView indexPathForCell:myCell];  
NSIntage i = cellPath.row; //这个就是cell的indexPath.row;

}


0 0
原创粉丝点击