tounaobun tounaobun - 6 months ago 123
iOS Question

Pass UICollectionView touch event to its parent UITableViewCell

Here is the view structure.The outer view is a UITableView,inside the UITableViewCell,there is a UICollectionView nested in it.And notice that there is some spacing(black) between UICollectionViewCells.When I tap the spacing in the UICollectionView, I want the touch event pass to the UITableViewCell.

enter image description here

Answer

After google around, I have found a solution.Just inherit UICollectionView class and override hitTest:withEvent method.

CustomCollectionView.h

@interface CustomCollectionView : UICollectionView

@end

CustomCollectionView.m

@implementation CustomCollectionView

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {

    UIView *hitView = [super hitTest:point withEvent:event];

    if ([hitView isKindOfClass:[self class]]) {
        // If it is class UICollectionView,just return nil.
        return nil;
    }
    // else return super implementation.
    return [super hitTest:point withEvent:event];
}

@end