UICollectionView didselect不起作用

时间:2013-06-17 19:33:11

标签: 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。

2 个答案:

答案 0 :(得分:9)

您已实施didDeselectItemAtIndexPath而不是didSelectItemAtIndexPath,因此当您选择其他单元格时,您首先取消选择的单元格会触发didDeselectItemAtIndexPath

答案 1 :(得分:0)

使用了错误的方法。didSelectItemAtIndexpath的定义不是didDeselect