scrrr
scrrr

Reputation: 5340

Forward tap-gesture to superview of UICollectionViewCell

I have a UITableView where each UITableViewCell contains a UICollectionView. The UICollectionView has UICollectionViewCells.

My problem is: Some of these UICollectionViewCells should react to tap-gestures, others should forward the event to the UITableViewCell (so it triggers displaying of the detail-view for that UITableViewCell).

I've been studying the apple-docs and several questions here but I can't get it to work.

(I think it should be possible to solve this generally, but just in case: Each UICollectionViewCell contains an UIImageView.)

Suggestions are very appreciated.

Upvotes: 4

Views: 2066

Answers (2)

Timothy Moose
Timothy Moose

Reputation: 9915

If I understand you correctly, you can do it like this:

  1. Set cell.userInteractionEnabled = NO on the cells you don't want to handle events.
  2. Override hitTest in your UICollectionView with this method:

    -(UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
    {
        UIView *hitTest = [super hitTest:point withEvent:event];
        return hitTest == self ? nil : hitTest;
    }
    

So tapping anywhere outside of a cell with user interaction enabled, super returns the collection view and we return nil, causing the table view cell to handle the event.

Upvotes: 1

Felix Lamouroux
Felix Lamouroux

Reputation: 7494

Why don't you let only the uicollectionview cell's handle the taps and then in the callback determine what action to take. For some you could do what you do now when tapping the cell, for others whatever you want.

Upvotes: 0

Related Questions