解决UITableViewCell点击无效即不执行didSele
2020-04-01 本文已影响0人
小蜜蜂Bee
有时候确实挺奇怪的,按tableView的用法,实现了tableView的代理方法之后,点击cell应该是会执行didSelectRowAtIndexPath方法的,不过事情就是这么巧!有时候是真的没执行,那么现在我们就来讲讲如何解决点击cell不执行didSelectRowAtIndexPath方法的问题。
步骤1:
在cellForRowAtIndexPath方法里面创建cell的时候,给cell的contentView添加一个点击手势,示例代码如下:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
XMFIdeaDetailCommentCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([XMFIdeaDetailCommentCell class])];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.delegate = self;
[self setCommentModelOfCell:cell atIndexPath:indexPath];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
//解决cell点击无效的问题
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
[cell.contentView addGestureRecognizer:tap];
return cell;
}
步骤2:
执行点击手势绑定的方法,示例代码如下:
//点击手势绑定的方法
-(void)tapAction:(UITapGestureRecognizer *)tap{
CGPoint point = [tap locationInView:self.myTableView];
NSIndexPath *indexPath = [self.myTableView indexPathForRowAtPoint:point];
XMFIdeaDetailCommentCell *cell = [self.myTableView cellForRowAtIndexPath:indexPath];
//判断手势点击的点是否在按钮视图内
CGRect rc = [cell convertRect:cell.replyBtn.frame toView:self.myTableView];
if (CGRectContainsPoint(rc, point)) {
DLog(@"触摸点在按钮视图内");
}else{
}
}
这样就可以解决点击cell不执行didSelectRowAtIndexPath方法的问题了,搞定!
如果以上的方法帮助到你了,欢迎分享,更欢迎赞赏,也可以直接打开支付宝、微信、QQ的扫一扫功能直接扫下面的支付宝、微信、QQ三合一打赏码进行打赏支持作者创作,感谢感谢!
赞赏码欢迎和我交流,微信与QQ同号:834537795(小蜜蜂)