56

I am attempting to update a single cell inside a UICollectionView, specifically I am just trying to update an image in that specific cell detonated by cell.imageView. [self.collectionView reloadData] is not really an option because it makes the whole collection view flash. [self.collectionView beginUpdates] is not a thing in a UICollectionViewit seems.

I understand I may be able to use:

[self.collectionView performBatchUpdates:^{
    //do something
} completion:nil];

I am not exactly sure what to put inside a completion block to update that certain cell's imageView. This is all being done inside of didSelectItemAtIndexPath.Also I am not using NSFetchedResultsController. Any ideas?

2
  • how about invalidateLayout method in your collectionViewLayout? Nov 30, 2013 at 5:46
  • @DineshRaja invalidateLayout doesn't reload the cell's content because the "cellForItemAt indexPath" isn't called.
    – Kappe
    Mar 12, 2018 at 11:31

3 Answers 3

79

- (void)reloadItemsAtIndexPaths:(NSArray *)indexPaths

Here it is a method to reload the specific indexPaths in your collectionView

3
  • Thank you! that ended up working. Now let's say I have a collectionView of 50 cells, and i just in data for another 50 cells, how to i add those 50 extra cells and display the data in them without doing a reloadData? Doing a reloadData results in an unnecessary flash of all the images in the collectionView and I would like to avoid that Nov 30, 2013 at 21:52
  • you have to call reloadData, if your array changes by any mean. because collection view don't have any idea, if you didn't call reloadData method. May be invalidateLayout method in collectionViewLayout works. But you don't have any other option. Dec 2, 2013 at 7:41
  • It can be a little tricky to implement correctly, but you could always use -[UICollectionView insertItemsAtIndexPaths:] to insert the cells in the collection view after you updated the data source.
    – Dillan
    Jun 19, 2015 at 23:47
42

Dinesh's answer is spot-on. But to avoid unwanted animations when reloading (aka "flashing") use:

BOOL animationsEnabled = [UIView areAnimationsEnabled];
[UIView setAnimationsEnabled:NO];
[myCollectionView reloadItemsAtIndexPaths:myIndexPaths];
[UIView setAnimationsEnabled:animationsEnabled];
2
  • I doesn't seem that I can assign a UIVIew to a BOOL. Dec 2, 2013 at 2:35
  • 1
    @Unome It's a class method so no specific view.
    – gavdotnet
    Apr 15, 2015 at 0:11
5

See Inserting, Deleting, and Moving Sections and Items from the "Collection View Programming Guide for iOS":

To insert, delete, or move a single section or item, you must follow these steps:


Update the data in your data source object. Call the appropriate method of the collection view to insert or delete the section or item. It is critical that you update your data source before notifying the collection view of any changes. The collection view methods assume that your data source contains the currently correct data. If it does not, the collection view might receive the wrong set of items from your data source or ask for items that are not there and crash your app.


So in your case, you must add an image to the collection view data source first and then call insertItemsAtIndexPaths. The collection view will then ask the data source delegate function to provide the view for the inserted item.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.