I have a UICollectionView which is about the size of the screen. The UICollectionViewCells that it displays are the same size as the collectionView. Each cell has a UIImage
In case it helps, this just bite me in a silly way spending a couple of hours. Double check if your custom cell is subclassing UICollectionViewCell instead of, let's say, UICollectionReusableView
I ran into this problem in Swift 3 and xcode 8 , I had a view and a collection view inside this.The collection view has userInteractionEnabled to true , still it was not working.Fixed this issue by overriding shouldHighlightItemAt , I dont have any extra / custom implementation in this method , so i did not override this method.After adding the below code the didSelectItem method is called.
func collectionView(_ collectionView: UICollectionView, shouldHighlightItemAt indexPath: IndexPath) -> Bool {
return true
}
Just add to the already resolved question one more situation where the tap gesture might not work, since this did keep tormenting me.
If you return false
within UICollectionViewDelegate'
s collectionView:shouldHighlightItemAtIndexPath:
, the tap gesture won't function and the collectionView:didSelectItemAtIndexPath:
would not be called. The default return value of this delegate method is true
, so you won't have the problem if you don't implement it deliberately.
I faced a similar problem and the culprit in my case turned out to be the CardView I was using in UICollectionView Cell's heirarchy. As soon as I disabled the user interaction for the CardView the collectionView started responding to the touch events.
If you have a view on the cell which obstructs the cells content view that is intractable then you will not be able to hook into the delegate callback for the cell.
You will want to disable user interaction on the obstructing view either in the NIB or in the code.
view.userInteractionEnabled = NO;
I just ran into this problem myself. Originally I had buttons and labels, then I refactored my UI and turned those button/labels into cells for a UICollectionView
.
I eventually realized the buttons were consuming my taps. I had unthinkingly just moved my original buttons into the cell, and not turned it into a straight UIImage
. I had destroyed the actions associated buttons and was just looking for cell selection so I took me a while to figure it out.
Stupid and obvious in retrospect, but took me a couple hours to realize what I had done.