11

I am trying to follow the following post on reload a cell in a 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?

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.

Thanks in advance for your help!

Community
  • 1
  • 1
scientiffic
  • 9,045
  • 18
  • 76
  • 149

6 Answers6

20

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]

GeneCode
  • 7,545
  • 8
  • 50
  • 85
Daniel Galasko
  • 23,617
  • 8
  • 77
  • 97
  • 2
    Should be `[collectionView performBatchUpdates:^{ [collectionView reloadItemsAtIndexPaths:@[[NSIndexPath indexPathForRow:indexPath.row inSection:0]]]; } completion:nil];` – jose920405 Mar 18 '16 at 21:20
  • No it shouldn't @jose920405. In your example you are creating an index path from an already existing index path! Just go reloadItemsAtIndexPaths:@[indexPath] please look through my solution again, there is nothing wrong – Daniel Galasko Mar 19 '16 at 05:28
  • Your code is wrong. Missing brackets and such. I edited it to make it right. – GeneCode Feb 21 '17 at 10:03
8

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.. :)

Rashad
  • 11,057
  • 4
  • 45
  • 73
2

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)
}
Ashish
  • 2,977
  • 1
  • 14
  • 32
1

You can use this way for Swift 5.1:

collectionViewOutletName.reloadItems(at: [IndexPath(row: indexNumber, section: sectionNumber)] )
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 29 '21 at 06:59
0

You can use this way for Swift 5:

CollectionView.reloadItems(at: IndexPath])
batuhankrbb
  • 598
  • 7
  • 10
-2
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *dic = @{@"option":[NSNumber numberWithBool:YES]};
    [self.array insertObject:dic atIndex:indexPath.row];

    [collectionView reloadItemsAtIndexPaths:@[indexPath]];
}
Pang
  • 9,564
  • 146
  • 81
  • 122
Bharrath
  • 79
  • 1
  • 5