iOS:重新加载UICollectionView的单个单元格

我正试图按照下面的post重新加载UICollectionView中的单元格:

UICollectionView更新单个单元格

我知道我最终想要看起来像这样的东西:

[self.collectionView reloadItemsAtIndexPaths:??]; 

但我不知道如何find并传递特定单元格的indexPath。 任何人都可以提供任何指针?

我有一个NSMutableArray包含所有的单元格信息(在每个单元格中生成的imageViews)。 理想情况下,我想要做的是通过NSMutableArray的索引刷新页面上的相应单元格。 但我不知道如何将数字索引转换为索引path。

在此先感谢您的帮助!

这取决于你的实现,以及你如何将你的单元格分为几个部分,但是假设你有一个部分,数组中的每个元素对应于你的表中的一行。

如果你想重新加载一个对应于索引x的数组元素的单元格,所有你需要做的就是写入

 [_collectionView performBatchUpdates:^{ [_collectionView reloadItemsAtIndexPaths:@[[NSIndexPath x inSection:0]]]; } completion:^(BOOL finished) {}]; 

相反,如果你有这个单元格但是想要find它的索引path,你总是可以调用[collectionView indexPathForCell:myCell]

玩具必须传递包含要重新加载的indexPath的数组。

 [self.collectionView reloadItemsAtIndexPaths: indexpathArray]; 

NSIndexPath有一些属性名称部分和行,都表示一个单元格。 如果你的集合视图有单独的部分,你可以通过设置section = 0来创build一个NSIndexPath ;

 NSIndexPath *indexPath = [NSIndexPath indexPathForRow:rowIndex inSection:0]; 

这里rowIndex是你的数组索引。 顺便说一句,你必须与你新创build的索引path,然后传递给reloadItemsAtIndexPaths

希望这可以帮助.. :)

 - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { NSDictionary *dic = @{@"option":[NSNumber numberWithBool:YES]}; [self.array insertObject:dic atIndex:indexPath.row]; [collectionView reloadItemsAtIndexPaths:@[indexPath]]; }