iphone - UICollectionView didselect 不起作用

标签 iphone ios uicollectionview reloaddata didselectrowatindexpath

每次我按下一个单元格时,我都希望该单元格更改图像:

- (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];
  }

我按下一个单元格但没有任何反应,现在如果我按下另一个单元格,前一个单元格会改变图像并且我按下的每个单元格都会发生同样的事情。更改的图像始终是前一个单元格的图像。好像它没有更新 collectionView,因为我再次按下 collectionView。

最佳答案

您实现的是 didDeselectItemAtIndexPath 而不是 didSelectItemAtIndexPath,因此当您选择其他单元格时,您首先选择的单元格将被取消选择并触发 didDeselectItemAtIndexPath

关于iphone - UICollectionView didselect 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17155340/

相关文章:

iphone - 行动 :@selector(showAlert:) how to pass parameter in this showAlert method?

iPhone - 跟踪蜂窝数据使用情况

iphone - UIImageView 单元格 contentView 的 View 之一未与其父 View 一起扩展

ios - 将字符串转换为整数 iOS + Swift 2.0

c++ - Qt + iOS - AppDelegate 在哪里?

ios - 触摸时更改 UICollectionViewCell 的颜色

ios - 如何在两个 View Controller 之间传递字符串或数据对象?

ios - IOS 启动画面缩放问题

ios - UICollectionView cellForItem 调用不正确

iOS - 将 Segues 附加到我的 Collection View Cells