Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UICollectionView didselect doesn't work

Everytime I press a cell I want the cell to change image:

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

      if ([[arrayOfImages objectAtIndex:indexPath.item] isEqual: @"dog.png"]) {
          [arrayOfImages replaceObjectAtIndex:indexPath.item withObject:@"cat.png"];

      } else if ([[arrayOfImages objectAtIndex:indexPath.item] isEqual: @"cat.png"]) {
          [arrayOfImages replaceObjectAtIndex:indexPath.item withObject:@"lion.png"];

      } else if ([[arrayOfImages objectAtIndex:indexPath.item] isEqual: @"lion.png"]) {
          [arrayOfImages replaceObjectAtIndex:indexPath.item withObject:@"dog.png"];

      }

    [myCollectionView reloadData];
  }

I press a cell and nothing happen, now if I press another cell, the previous one changes image and the same thing happens for every cell I press. The image that change is always the image of the previous cell. It's like it doesn't update the collectionView since I press again the collectionView.

like image 817
Tortuga Avatar asked Aug 12 '26 08:08

Tortuga


1 Answers

You have implemented didDeselectItemAtIndexPath not didSelectItemAtIndexPath, thus when you select an other cell the cell you selected first gets deselected and triggers the didDeselectItemAtIndexPath.

like image 52
rckoenes Avatar answered Aug 14 '26 22:08

rckoenes



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!