tounaobun tounaobun - 4 months ago 90
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


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


@interface CustomCollectionView : UICollectionView



@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];