5

I'm using an UICollectionView added on top of a view with a single tap gesture recogniser. The CollectionView makes use of custom cells without any subviews. The delegate's method

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath

is only called when selecting the cell with two instead of one finger or when doing a LONG press using a single finger.

I'm not accidentally overriding

- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath.

I read through all similar questions which were all solved by removing some kind of view or gesture recogniser.

Any ideas what's wrong here?

11
  • 1
    I'm feeling uder water rocks, can you share a simple project with it behaviour?
    – Bimawa
    Commented Apr 11, 2017 at 11:07
  • post your for selection please Commented Apr 11, 2017 at 11:07
  • did you tried without didDeselectItemAtIndexPath ?
    – Jack
    Commented Apr 11, 2017 at 11:09
  • self.collectionView.allowsMultipleSelection = false try this Commented Apr 11, 2017 at 11:11
  • This link may helpful: stackoverflow.com/questions/16444688/… Commented Apr 11, 2017 at 11:11

3 Answers 3

6

The solution: The collection view was added on top of a view containing a single-tap gesture recogniser. This some how caused this behaviour. I removed the recogniser from the collection views parent view and it works.

Feel free to explain why this is expected behaviour. I would have argued that the top most view (CollectionView) handles the touches before they are passed to the view behind.

2
  • +1, this solved my problem. I'm also very curious why the collection view touch events aren't handled before they're passed on to the tap gesture recognizer though...
    – erikvdplas
    Commented Jul 21, 2017 at 12:38
  • this was exactly the issue with me Commented Apr 9, 2018 at 10:31
2

An alternative to removing the gesture completely is to set it so that it doesn't cancel touches in view.

UITapGestureRecognizer* tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(someSelector)];
tapRecognizer.cancelsTouchesInView = NO;
tapRecognizer.delaysTouchesBegan = NO;
tapRecognizer.delaysTouchesEnded = NO;
[self.view addGestureRecognizer:tapRecognizer];
0

Based on Christoph solution; I've come up with this code, which is removing all the gesture recognisers in my parent view.

for (UIGestureRecognizer *recognizer in self.view.gestureRecognizers) {
    [self.view removeGestureRecognizer:recognizer];
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.