每次我按下一个单元格时,我都希望该单元格更改图像:
- (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/