Views within custom UICollectionViewCell not receiving touch events
Asked Answered
G

3

8

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.

Gerick answered 23/3, 2014 at 9:54 Comment(0)
G
21

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...

Gerick answered 23/3, 2014 at 10:49 Comment(3)
Fantastic! Such an easy mistake to make and it's so hard to spot it.Watford
You, Sir, made my month. I cannot tell you how very much this helped. THANK YOU!Coffin
Thank you so much for your help @patschiboy, your memory shall serve as a blessing on StackOverflow foreverCommonly
L
0

a have the same problem for UIButton in my custom UICollectionViewCell

Adding that function to my UICollectionViewCell solved problem for me:

- (void)setBounds:(CGRect)bounds {
    [super setBounds:bounds];
    self.contentView.frame = bounds;
}
Lorri answered 16/10, 2014 at 21:45 Comment(0)
C
0

If you have a view/scrollView at the root and it is attached to backgroundView or selectedBackGroundView remove it and it will work fine.

Carrew answered 31/3, 2015 at 12:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.