在UITableView的Cell里嵌套使用CollectionView场景里,如果在点击CollectionView的空白处将点击事件传递到TableView的Cell Did Selected方法上?
方法如下:
方法一:给collectionView加手势,但是会出现与collectionview 的collectionCell didselect 事件冲突
解决办法:在tablveiwCell 中写出下代码
UITapGestureRecognizer *collectionvViewGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(collectionViewClick)];
collectionvViewGesture.delegate = self;
[self.collectionView addGestureRecognizer:collectionvViewGesture];
- (void)collectionViewClick{
if ([self.delegate respondsToSelector:@selector(cellCollectionViewClick:)]) {
[self.delegate cellCollectionViewClick:self];
}
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
// 输出点击的view的类名,则不截获Touch事件
// NSLog(@"%@", NSStringFromClass([touch.view class]));
if ([gestureRecognizer isKindOfClass:[UITapGestureRecognizer class]]) {
// 备注,我的collectionCell上是图片
if ([NSStringFromClass([touch.view class]) isEqualToString:@"UIImageView"]) {
return NO;
}
}
return YES;
}
方法二:(也是最好的解决方法) 可以利用响应链,让事件传递到tableview上
只用在tableviewCell中加如下方法即可
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{
UIView *view = [super hitTest:point withEvent:event];
if ([view isKindOfClass:[UICollectionView class]]) {
return self;
}
return [super hitTest:point withEvent:event];
}