iOS:重新加载 UICollectionView 的单个单元格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25707679/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iOS: reload single cell of UICollectionView
提问by scientiffic
I am trying to follow the following post on reload a cell in a UICollectionView:
我正在尝试按照以下帖子重新加载 UICollectionView 中的单元格:
UICollectionView update a single cell
I know that I ultimately want something that looks like this:
我知道我最终想要看起来像这样的东西:
[self.collectionView reloadItemsAtIndexPaths:??];
But I don't know how to find and pass the indexPath of a particular cell. Can anyone provide any pointers?
但我不知道如何查找并传递特定单元格的 indexPath。任何人都可以提供任何指示吗?
I have an NSMutableArray that contains all the cell information (imageViews that are generated in each cell). Ideally, what I'd like to do is pass the index of the NSMutableArray to refresh the corresponding cell on the page. But I'm not sure how the numerical index gets translated into an indexPath.
我有一个 NSMutableArray 包含所有单元格信息(在每个单元格中生成的图像视图)。理想情况下,我想做的是传递 NSMutableArray 的索引以刷新页面上的相应单元格。但我不确定数字索引如何转换为 indexPath。
Thanks in advance for your help!
在此先感谢您的帮助!
回答by Daniel Galasko
It depends on your implementation and how you have grouped your cells into sections but let's assume you have one section and each element in your array corresponds to a row in your table.
这取决于您的实现以及您如何将单元格分组为多个部分,但假设您有一个部分,并且数组中的每个元素对应于表格中的一行。
If you wanted to reload a cell corresponding to an array element at index x
all you need to do is write
如果您想重新加载与索引处的数组元素对应的单元格,x
您需要做的就是编写
[_collectionView performBatchUpdates:^{
[_collectionView reloadItemsAtIndexPaths:@[[NSIndexPath x inSection:0]]];
} completion:^(BOOL finished) {}];
Conversely, if you have the cell but want to find its index path you can always call [collectionView indexPathForCell:myCell]
相反,如果您有单元格但想找到它的索引路径,您可以随时调用 [collectionView indexPathForCell:myCell]
回答by Rashad
Toy have to pass an array containing the indexPaths that you want to be reloaded.
Toy 必须传递一个包含要重新加载的 indexPaths 的数组。
[self.collectionView reloadItemsAtIndexPaths: indexpathArray];
NSIndexPath
has some property names section and row, both represents a cell. If your collection view has single section, you can create a NSIndexPath
by setting its section = 0;
NSIndexPath
有一些属性名称部分和行,都代表一个单元格。如果您的集合视图只有一个部分,您可以NSIndexPath
通过设置它的部分 = 0来创建一个;
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:rowIndex inSection:0];
Here rowIndex
is your array index. Btw you have to make an array with you newly created indexpaths, then pass it to reloadItemsAtIndexPaths
.
这rowIndex
是您的数组索引。顺便说一句,您必须使用新创建的索引路径创建一个数组,然后将其传递给reloadItemsAtIndexPaths
.
Hope this helps.. :)
希望这可以帮助.. :)
回答by Ashish
Obj-C
对象-C
NSMutableArray *indexPaths = [[NSMutableArray alloc] init]
[indexPaths addObject:indexPath];
[self.collectionView reloadItemsAtIndexPaths: indexPaths];
Swift 4.0
斯威夫特 4.0
var indexPaths = [IndexPath]()
indexPaths.append(indexPath)
if let collectionView = collectionView {
collectionView.reloadItemsAtIndexPaths(indexPaths)
}
回答by Bharrath
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
NSDictionary *dic = @{@"option":[NSNumber numberWithBool:YES]};
[self.array insertObject:dic atIndex:indexPath.row];
[collectionView reloadItemsAtIndexPaths:@[indexPath]];
}