UICollectionView only calling didSelectItemAtIndexPath if user double taps, will not call when user single taps

IosObjective CIpadUicollectionviewUicollectionviewcell

Ios Problem Overview


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 which is the size of the cell. The CollectionView has paging enabled so essentially it is a full screen photo slideshow that the user can swipe through.

The problem is that
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath is only being called if the user taps with two fingers on a cell or long presses with one finger and then releases. It does not seem to have the default behaviour of single tap selection. I have not made any changes to the CollectionView gesture recognizer so am having trouble finding a solution to this problem.

Ios Solutions


Solution 1 - Ios

Are you sure you're not accidentally overriding - (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath? "Select" vs. "deselect" has tripped me up in the past with Xcode's code completion.

Solution 2 - Ios

I was just having the same problem and it turned out that there was a UITapGestureRecognizer on the UIView containing the UICollectionView and it was responding instead.

That explains why didSelectItemAtIndexPath works only if the user taps with two fingers or long presses with one finger because that doesn't trigger the UITapGestureRecognizer.

So, check all the UITapGestureRecognizer you got there, not necessarily on the UICollectionView directly but it could be on any UIView containing it.

Solution 3 - Ios

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.

Solution 4 - Ios

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.

Solution 5 - Ios

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.

Solution 6 - Ios

If you use only didselect:

self.collectionView.allowsMultipleSelection = false

Or delete that line.

Solution 7 - Ios

I just had the same problem...I was overriding touchesBegan in the CollectionCell view and this caused the didSelectItemAtIndexPath to not fire when I touched the cell. Removed the override and all worked.

Solution 8 - Ios

In my case it was just

self.collectionView.allowsSelection  =NO;

I had in code.

changing to

self.collectionView.allowsSelection  =YES;

fix it.

Solution 9 - Ios

Try removing all of your UIGestures from the view then test to see if the UICollectionView Cells are able to be interacted with normally.

In my case, I had to remove the lines:

let tap = UITapGestureRecognizer(target: self, action: #selector(dismissKeyboard))
    self.view.addGestureRecognizer(tap)

Solution 10 - Ios

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
    }

Solution 11 - Ios

I'm happened to stumble upon this problem, and it frustrated me one long day until I found this You just need to uncheck "User Interaction Enabled" when I selected the Cell on the top left inside the CollectionView in Storyboard. and problem solved

Solution 12 - Ios

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.

Solution 13 - Ios

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 

Solution 14 - Ios

Remember to add UICollectionViewDataSource, UICollectionViewDelegate to the class

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKris GellciView Question on Stackoverflow
Solution 1 - IosOwen MathewsView Answer on Stackoverflow
Solution 2 - IosiDifferentView Answer on Stackoverflow
Solution 3 - IosOliver AtkinsonView Answer on Stackoverflow
Solution 4 - IosBlaszardView Answer on Stackoverflow
Solution 5 - IosDBDView Answer on Stackoverflow
Solution 6 - IosSour LeangChheanView Answer on Stackoverflow
Solution 7 - IosdigthewellsView Answer on Stackoverflow
Solution 8 - Iosuser1105951View Answer on Stackoverflow
Solution 9 - IosdrfalcoewView Answer on Stackoverflow
Solution 10 - IosVinodha SundaramoorthyView Answer on Stackoverflow
Solution 11 - IosPhan HoangView Answer on Stackoverflow
Solution 12 - IosLove KumarView Answer on Stackoverflow
Solution 13 - IosnandodelauniView Answer on Stackoverflow
Solution 14 - IosTylerCView Answer on Stackoverflow