场景:需要封装一个弹窗,任何时候都可以从底部弹出,向外暴露内容数组通过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 ,然后重写代理方法
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 ;
// 若为 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
类似的效果