UICollectionView reloadData not functioning properly in iOS 7
I've been updating my apps to run on iOS 7 which is going smoothly for the most part. I have noticed in more than one app that the reloadData
method of a UICollectionViewController
isn't acting quite how it used to.
I'll load the UICollectionViewController
, populate the UICollectionView
with some data as normal. This works great on the first time. However if I request new data (populate the UICollectionViewDataSource
), and then call reloadData
, it will query the data source for numberOfItemsInSection
and numberOfSectionsInCollectionView
, but it doesn't seem to call cellForItemAtIndexPath
the proper number of times.
If I change the code to only reload one section, then it will function properly. This is no problem for me to change these, but I don't think I should have to. reloadData
should reload all visible cells according to the documentation.
Has anyone else seen this?
Solution 1:
Force this on the main thread:
dispatch_async(dispatch_get_main_queue(), ^ {
[self.collectionView reloadData];
});
Solution 2:
In my case, the number of cells/sections in the datasource never changed and I just wanted to reload the visible content on the screen..
I managed to get around this by calling:
[self.collectionView reloadItemsAtIndexPaths:[self.collectionView indexPathsForVisibleItems]];
then:
[self.collectionView reloadData];
Solution 3:
I had exactly the same issue, however I managed to find what was going on wrong. In my case I was calling reloadData from the collectionView:cellForItemAtIndexPath: which looks not to be correct.
Dispatching call of reloadData to the main queue fixed the problem once and forever.
dispatch_async(dispatch_get_main_queue(), ^{
[self.collectionView reloadData];
});
Solution 4:
Reloading some items didn't work for me. In my case, and only because the collectionView I'm using has just one section, I simply reload that particular section. This time the contents are correctly reloaded. Weird that this is only happening on iOS 7 (7.0.3)
[self.collectionView reloadSections:[NSIndexSet indexSetWithIndex:0]];