UICollectionView update a single cell

IosObjective CUitableviewUicollectionviewUicollectionviewcell

Ios Problem Overview


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?

Ios Solutions


Solution 1 - Ios

- (void)reloadItemsAtIndexPaths:(NSArray *)indexPaths

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

Solution 2 - Ios

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];

Solution 3 - Ios

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPaulius DragunasView Question on Stackoverflow
Solution 1 - IosDinesh RajaView Answer on Stackoverflow
Solution 2 - IosgavdotnetView Answer on Stackoverflow
Solution 3 - IosVizllxView Answer on Stackoverflow