背景视图上点击事件与tableView冲突的解决办法


场景:需要封装一个弹窗,任何时候都可以从底部弹出,向外暴露内容数组通过block回传点击的index. 在封装类中放了一个背景view,半透明黑色,给背景视图加点击手势用于退出弹框,背景视图上加了tableview,然后发现tableview的cell点击事件与背景视图的tap事件冲突,点击tableview时直接响应了背景视图(父视图)的点击方法。

即:背景视图上有 Tap 点击手势,背景视图上添加了 tableView 后, tableVIew 的点击事件 - ( void ) tableView: ( UITableView * ) tableView didSelectRowAtIndexPath: ( NSIndexPath * ) indexPath 不执行。
原因:响应链与手势冲突,具体可自行百度响应链与手势的执行
解决办法:
1.
cell 添加点击手势,通过手势的方法实现
-
( UITableViewCell * ) tableView: ( UITableView * ) tableView cellForRowAtIndexPath: ( NSIndexPath * ) indexPath {
   
    UITableViewCell *cell =
[ tableView dequeueReusableCellWithIdentifier:reusedCell ];
    cell.separatorInset = UIEdgeInsetsMake
( 0 , 0 , 0 , 0 );
    cell.contentView.backgroundColor = ZCHexColor
( @"#f2f2f2" );
   
    UITapGestureRecognizer *tap =
[[ UITapGestureRecognizer alloc ] initWithTarget:self action:@selector ( labelAction: )];
    UILabel *title =
[[ UILabel alloc ] initWithFrame:CGRectMake ( 0 , 0 , LMWID-20 , 54 )];
    title.backgroundColor =
[ UIColor clearColor ];
    title.tag = 1022 + indexPath.row
;
   
[ title addGestureRecognizer:tap ];
    title.userInteractionEnabled = YES
;
    title.textColor = ZCHexColor
( @"#333333" );
    title.textAlignment = NSTextAlignmentCenter
;
   
[ cell.contentView addSubview:title ];
    title.text =
[ NSString stringWithFormat:@"%@" , _dataSource [ indexPath.row ]];
   
    cell.textLabel.text = @"
舒服舒服 " ;
     return cell
;
   
}

-
( void ) labelAction: ( UIGestureRecognizer * ) ges {
    if
( self.select ) {
        self.select
( ges.view.tag - 1022 );
       
[ self removeFromSuperview ];
    }
}

2.
tableView 添加点击手势,
UITapGestureRecognizer *tap =
[[ UITapGestureRecognizer alloc ] init ];
tap.delegate = self
;
[ self.tableView addGestureRecognizer:tap ];
遵循手势协议  UIGestureRecognizerDelegate  ,然后重写代理方法

- ( BOOL )gestureRecognizer:( UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:( UITouch *)touch {

   
// 若为 UITableViewCellContentView (即点击了 tableViewCell ),则不截获 Touch 事件
   
if ([ NSStringFromClass ([touch. view class ]) isEqualToString : @"UILabel" ]) {
       
       
CGPoint point = [touch locationInView : self . tableView ];
       
NSIndexPath *indexPath = [ self . tableView indexPathForRowAtPoint :point];
       
if ( self . select ) {
           
self . select (indexPath. row );
            [
self removeFromSuperview ];
        }
       
return NO ;
    }
   
return   YES ;
   
}

即可实现 didSelectRowAtIndexPath 类似的效果
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值