[英]iOS: reload single cell of UICollectionView
我正在尝试按照以下帖子重新加载 UICollectionView 中的单元格:
我知道我最终想要看起来像这样的东西:
[self.collectionView reloadItemsAtIndexPaths:??];
但我不知道如何查找并传递特定单元格的 indexPath。 任何人都可以提供任何指示吗?
我有一个 NSMutableArray,其中包含所有单元格信息(在每个单元格中生成的图像视图)。 理想情况下,我想做的是传递 NSMutableArray 的索引以刷新页面上的相应单元格。 但我不确定数字索引如何转换为 indexPath。
在此先感谢您的帮助!
这取决于您的实现以及您如何将单元格分组为多个部分,但假设您有一个部分,并且数组中的每个元素对应于表格中的一行。
如果您想重新加载对应于索引x
处的数组元素的单元格,您需要做的就是编写
[_collectionView performBatchUpdates:^{
[_collectionView reloadItemsAtIndexPaths:@[[NSIndexPath x inSection:0]]];
} completion:^(BOOL finished) {}];
相反,如果您有单元格但想找到它的索引路径,您可以随时调用[collectionView indexPathForCell:myCell]
Toy 必须传递一个包含要重新加载的 indexPaths 的数组。
[self.collectionView reloadItemsAtIndexPaths: indexpathArray];
NSIndexPath
有一些属性名称部分和行,都代表一个单元格。 如果你的集合视图只有一个部分,你可以通过设置它的部分 = 0 来创建一个NSIndexPath
;
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:rowIndex inSection:0];
这里rowIndex
是您的数组索引。 顺便说一句,您必须使用新创建的索引路径创建一个数组,然后将其传递给reloadItemsAtIndexPaths
。
希望这可以帮助.. :)
对象-C
NSMutableArray *indexPaths = [[NSMutableArray alloc] init]
[indexPaths addObject:indexPath];
[self.collectionView reloadItemsAtIndexPaths: indexPaths];
斯威夫特 4.0
var indexPaths = [IndexPath]()
indexPaths.append(indexPath)
if let collectionView = collectionView {
collectionView.reloadItemsAtIndexPaths(indexPaths)
}
您可以将这种方式用于 Swift 5:
CollectionView.reloadItems(at: IndexPath])
您可以将这种方式用于 Swift 5.1 :
collectionViewOutletName.reloadItems(at: [IndexPath(row: indexNumber, section: sectionNumber)] )
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
NSDictionary *dic = @{@"option":[NSNumber numberWithBool:YES]};
[self.array insertObject:dic atIndex:indexPath.row];
[collectionView reloadItemsAtIndexPaths:@[indexPath]];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.