1

When i tap one of UICollectionView's cell more than once-double tap, triple tap-, it's delegate method didSelectItemAtIndexPath also get called more than once. What can be the slickest way to prevent it?

I would appreciate any comments.

cihangirs
  • 55
  • 1
  • 12

2 Answers2

2

You can use your model object to hold selected property in it (or you can create a boolean array for only this purpose) . And check it in shouldSelectItemAtIndexPath method.

@cihangirs code:

- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath { 
    if (someModel.isSelected) { 
        return NO; 
    } else { 
        someModel.isSelected = YES; 
        return YES; 
    } 
}
Ersin Sezgin
  • 640
  • 5
  • 15
0

This is safest way to do your objective:-

 (void)collectionView:(UICollectionView *)collectionView
didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
  if([[collectionView indexPathsForSelectedItems] containsObject:indexPath]) // checking whether cell is already selected or not
    {
      return;
    }
 else
   {
 // do whatever you want to do on selection of cell

   }
}

The thing happening here is, whenever you select a cell it automatically get stored "indexPathsForSelectedItems" of Collection view, so the next time you tap on the selected cell again this method [[collectionView indexPathsForSelectedItems] containsObject:indexPath] will check whether that cell is already selected or not , if yes then it will return the method so that it doesnot go any step further.

Vizllx
  • 9,135
  • 1
  • 41
  • 79