iOS: reload single cell of UICollectionView

早过忘川 提交于 2019-12-01 03:37:19
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

[_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]

Toy have to pass an array containing the indexPaths that you want to be reloaded.

[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 *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.

Hope this helps.. :)

Obj-C

NSMutableArray *indexPaths = [[NSMutableArray alloc] init]    
[indexPaths addObject:indexPath];
[self.collectionView reloadItemsAtIndexPaths: indexPaths];

Swift 4.0

var indexPaths = [IndexPath]()
indexPaths.append(indexPath) 

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

    [collectionView reloadItemsAtIndexPaths:@[indexPath]];
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!