peko
peko

Reputation: 11335

How to pass gestures from UITextView to UICollectionViewCell

I have a horizontal scrolling UICollectionView with UICollectionViewCells that contain a UITextView. Is there any way to pass gestures on the textview to the cells, so that didSelectItemAtIndexPath gets called?. I tried it with subclassing UITextView and passing touchesbegin/end to the cell, but that didn't worked.

Upvotes: 2

Views: 2236

Answers (3)

dhmspector
dhmspector

Reputation: 99

This doesn't seem to work in iOS6.x: the all view in a UICollectionViewCell seem to be embedded in a UIView that is the first child of the cell. In order to get the actual cell that is the UITextView is in you will need to dereference a second time. In other words the order is (from bottom to top):

UITextView->enclosingUIView->UICollectionViewCell

Upvotes: 0

jjv360
jjv360

Reputation: 4140

You can make the view non-interactive, which will cause touches to get passed through:

textView.userInteractionEnabled = NO;

If you need it to be interactive, you can try this:

textView.editable = NO;
UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped)];
[textView addGestureRecognizer:tap];

... and then add this function to your UICollectionViewCell subclass:

-(void) tapped {
    UICollectionView *collectionView = (UICollectionView*)self.superview;
    NSIndexPath *indexPath = [collectionView indexPathForCell:self];
   [collectionView.delegate collectionView:collectionView didSelectItemAtIndexPath:indexPath];
}

I haven't tested it though...

Upvotes: 6

Mick MacCallum
Mick MacCallum

Reputation: 130222

Well, if your cell is the superview of the text view, you could implement something like this in the UITextViewDelegate method textViewDidBeginEditing:.

- (void)textViewDidBeginEditing:(UITextView *)textView {
    NSIndexPath *indexPath = [self.collectionView indexPathForCell:(UICollectionViewCell *)textView.superview];
    [self.collectionView selectItemAtIndexPath:indexPath animated:YES scrollPosition:UICollectionViewScrollPositionTop];
}

Upvotes: 0

Related Questions