Reloading CollectionViews

Image

Just fixed a crash related to UICollectionView which was due to the manner in which cells were being reloaded in the app.

It was a case when you have pull to refresh as well as data being requested from server after a specific page size. My last post objc_msgSend goes BOOM! reminded me to check “Log exceptions” and “Enable Zombies” within the schemes. What I got to know from that was that the issue was while the reload cells on CollectionView was being called and the dataSource was still being modified.

So in order to fix this, from now on :

Instead of :

//Reload CollectionView

[self.collectionView reloadData];

I would rather do :

//Reload CollectionView

NSIndexSet *indexSet = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, self.collectionView.numberOfSections)];

NSMutableArray *indexPaths = [[NSMutableArray alloc] init];

for (int i = 0; i < manager.newsItems.count; i++) {

NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];

[indexPaths addObject:indexPath];

}

[self.collectionView reloadSections:indexSet];

[self.collectionView reloadItemsAtIndexPaths:indexPaths];
Advertisements

About the post

iOS

%d bloggers like this: