Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UICollectionView didSelectItemAtIndexPath not called when tapped on UITextView

I have a UICollectionView with custom cells- They have a UITextView that mostly covers the entire cell. This presents a problem when using didSelectItemAtIndexPath. The only way to trigger it is by tapping outside the UITextView. I want it to trigger wherever in the cell you tap, whether there is a text view or not. How can this be done?

like image 756
imas145 Avatar asked Feb 23 '14 15:02

imas145


4 Answers

didSelectItemAtIndexPath is called when none of the subView of collectionViewCell respond to that touch. As the textView respond to those touches, so it won't forward those touches to its superView, so collectionView won't get it.

override hitTest:withEvent method of your collectionViewCell or CollectionView subclass and always return self from them.so it explicitly makes collectionView as first responder.

like image 140
santhu Avatar answered Nov 05 '22 03:11

santhu


I would suggest to use UIGestureRecognizer for each cell and when it taped to send it to UITextView or whatever , perhaps there maybe a better solutions , but I would use this 1 because of simplicity reasons.

like image 22
Roma-MT Avatar answered Nov 05 '22 03:11

Roma-MT


Do you override touchesEnded: withEvent: ?

I had the same problem today and I found that I have some customised logic in touchesEnded in one of collectionview's container views, and I didn't call

 [super touchesEnded: withEvent:]

when I'm done with my customised logic in touchesEnded.

After adding the super call, everything is fine.

like image 2
frankli Avatar answered Nov 05 '22 02:11

frankli


Select UITextView, in that specific case UICollectionViewCell, and switch to attribute inspector. The uncheck User interaction enabled and it should work fine.

like image 1
Stefano Mtangoo Avatar answered Nov 05 '22 03:11

Stefano Mtangoo