Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Views within custom UICollectionViewCell not receiving touch events

I have a UICollectionView which supports horizontal and vertical scrolling. Each collection view cell is a subclass of UICollectionViewCell and they are loaded from a NIB that contains UIButton's, UITableView's and even another UICollectionView. They are all inside a TPKeyboardAvoidingScrollView.

Scrolling works perfectly both horizontally and vertically, but it is not receiving touch events on the elements on the individual UICollectionViewCell. When I touch any of the elements, say a button that's part of the UICollectionViewCell, then all that I get is the didSelectItemAtIndexPath event on the UICollectionViewDelegate - I am unable to get any touch event passed through to one of the subviews of the collection view cell the tap occurs on.

like image 690
patschiboy Avatar asked Mar 23 '14 09:03

patschiboy


1 Answers

After going through some other stackoverflow articles I eventually found what caused my problem - the XIB file I used to initialise the UICollectionViewCell had a normal UIView element at the root; even though it was marked as a subclass of a UICollectionViewCell and behaved fine in any other way, that prevented the events to flow through.

So - if your custom UICollectionViewCell XIB file looks something like this at the the root (see the icon next to the Data Entry Matrix View, which is the name for my UICollectionViewCell subclass), then you will have the same problem:

enter image description here

My solution was to add a proper UICollectionViewCell class to the XIB, then move all the subviews under the existing Data Entry Matrix View into it, delete the old root, then reconnecting the Outlets. The end result looks like this:

enter image description here

You can see the different icon used - that's what you want. Once I'd change my XIB like this the events were passed through.

In the raw XIB file (view as Source), the non-working version introduced the root element as

<view ...>

whereas the working version changed that root element to

 <collectionViewCell ...>

Hope this saves someone else a couple of hours...

like image 161
patschiboy Avatar answered Nov 11 '22 23:11

patschiboy