Pass the UICollectionView touch event to its parent UITableViewCell

Here is the presentation structure:

Appearance - UITableView .

Inside the UITableViewCell there is a UICollectionView . And note that there is a black space between the cells in the collection view.

When I touch an interval in a UICollectionView, I want the touch event to jump to a UITableViewCell .

screenshot

+7
ios uitableview uicollectionview
source share
3 answers

After google, I found a solution. Just inherit the UICollectionView class and override the 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 
+5
source share

Great answer from @tounaobun. Tested and works as expected:

1) If you click anywhere in the collection that is not an item, then the table cell below it will select only a penalty.

2) if you click on the elements in the collection view, then the table cell will not be selected, and you can interact normally with the collection view (for example, scrolling, calling didSelectItem, etc.)

I turned to quick for reference:

Swift 3:

 class TableCellCollectionView: UICollectionView { override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? { if let hitView = super.hitTest(point, with: event) { if hitView is TableCellCollectionView { return nil } else { return hitView } } else { return nil } } 

Just add this class definition to your code (I have it in the utility file), and then change the collection view class from UICollectionView to TableCellCollectionView, and you should be set up.

+8
source share

Is it possible to set the collectionView backgroundView UserInteraction parameter, but make sure that the collection ViewCell has completed so that it can pass tapEvent to the next responder.

 - (void)viewDidLoad { theCollectionView.userInteractionEnabled = NO; } 

I hope this helps.

0
source share

All Articles