tableViewCell上collectionView空白部分点击事件穿透到 tablveiwcell上

第一种(比较笨的)实现方法:给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 的cell didselct方法上

只用在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];
}




你可能感兴趣的:(iOS开发技巧)