在自定义cell的时候,在cell上添加了一个button,然后在controller中调用这个button的时候要获取到cell
在iOS6中直接button.superView就可以
但是iOS7中不行。。。
上网查发现iOS7第一次的superview只能取到cell的content view也就说得取两次
但是结果发现还是不行,取两次竟然才取到cell的contentview层
不得已取三次superview实现
但是更新iOS8之后的调用发现崩溃···
检查发现三次取superview竟然取多了,到tableview层上了。。。
也就是说iOS8就是得取两次
·········
总之6取一次superview就行
7取三次superview
8取两次superview
建议使用 代理的方式 直接 找到cell的model 或者通过tag值的方式
cell添加button后通过button获取到cell
个人不喜欢用tag值的方法,再去写代理的话显得麻烦,然后偶然自己搜到一个简单的方法,与大家分享.
[cell.btn addTarget:self action:@selector(cellBtnClicked:event:) forControlEvents:UIControlEventTouchUpInside];
首先在方法后面添加一个事件event:
然后通过事件响应找到当前的indexPath
- (void)cellBtnClicked:(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)
{
// do something
}
}
总结一下:tableViewCell的 indexPath很神奇