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

sci*_*fic 10 objective-c nsindexpath ios uicollectionview

我试图按照以下帖子重新加载UICollectionView中的单元格:

UICollectionView更新单个单元格

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

[self.collectionView reloadItemsAtIndexPaths:??];
Run Code Online (Sandbox Code Playgroud)

但我不知道如何找到并传递特定单元格的indexPath.任何人都可以提供任何指示吗?

我有一个NSMutableArray,它包含所有单元格信息(在每个单元格中生成的imageViews).理想情况下,我想要做的是传递NSMutableArray的索引来刷新页面上的相应单元格.但我不确定数字索引如何转换为indexPath.

在此先感谢您的帮助!

Dan*_*sko 20

这取决于您的实现以及如何将单元格分组为多个部分,但我们假设您有一个部分,并且数组中的每个元素都对应于表中的一行.

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

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

相反,如果你有单元格但想要找到它的索引路径,你可以随时调用 [collectionView indexPathForCell:myCell]

  • 应该是`[collectionView performBatchUpdates:^ {[collectionView reloadItemsAtIndexPaths:@ [[NSIndexPath indexPathForRow:indexPath.row inSection:0]]]; }完成:nil];` (2认同)

Ras*_*had 8

玩具必须传递包含您要重新加载的indexPaths的数组.

[self.collectionView reloadItemsAtIndexPaths: indexpathArray];
Run Code Online (Sandbox Code Playgroud)

NSIndexPath有一些属性名称节和行,都代表一个单元格.如果您的集合视图具有单个部分,则可以NSIndexPath通过设置其section = 0 来创建;

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:rowIndex inSection:0];
Run Code Online (Sandbox Code Playgroud)

rowIndex是你的数组索引.顺便说一下你必须用新创建的索引路径创建一个数组,然后传递给它reloadItemsAtIndexPaths.

希望这可以帮助.. :)