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.
Asked
Active
Viewed 2,218 times
3 Answers
6
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...
-
yeah, but then i can't scroll. – peko Mar 28 '13 at 13:10
-
problem here is that selectItemAtIndexPath:animated:scrollPosition: does not cause any selection-related delegate methods to be called – peko Mar 28 '13 at 13:57
-
but [cv.delegate collectionView:cv didSelectItemAtIndexPath:ip]; does – peko Mar 28 '13 at 14:01
-
First part of answer works perfectly on iOS 8 if you don't need interaction. – user3344977 Jan 26 '15 at 01:46
0
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];
}

Mick MacCallum
- 129,200
- 40
- 280
- 281
-
textViewDidBeginEditing only gets called if the text is getting edited, but i don't wont the text to be editable. – peko Mar 28 '13 at 13:05
-
This does not work for me. I get nil for the indexPathForCell. I can get the cell, just not the indexPath. – Micah Jan 13 '14 at 19:47
-
1@Micah Sorry about that, I'm assuming you're using iOS 7? The view hierarchy has changed and the cell now exists at view.superview.superview. If you want a more dynamic solution, see my post here: http://stackoverflow.com/a/18255361/716216 – Mick MacCallum Jan 13 '14 at 20:20
-
Boom! @user716216 thanks! That saved me. Been on this for the past few hours. – Micah Jan 13 '14 at 21:27
0
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

dhmspector
- 89
- 4