I am having trouble with reuse cells and UICollectionView
on iOS 7. My code works fine on iOS 6 but in iOS 7 it does not reuse the cell after calling dequeueReusableCellWithReuseIdentifier
(do not call prepareForReuse
).
Even this code https://developer.apple.com/library/ios/samplecode/CollectionView-Simple/Introduction/Intro.html#//apple_ref/doc/uid/DTS40012860 does not reuse cell on iOS7 (but works well on iOS6), on every dequeueReusableCellWithReuseIdentifier
it creates a new cell and deallocates the old one. Is there some new stuff that prevents cells to be reused?
My cells are big enough that they it is very inefficient to not reuse them. I've notice lag on iOS 7, but not on iOS 6, because they are not being reused in iOS 7.
I was experiencing the same exact issue on my iPad 3 today (just one out of three iPad 3 I tested out), and I found out it is related to global Accessibility settings. The solution is to double check that every option in accessibility panel is disabled. I think that some options (like bigger fonts, for example) could stay enabled, but I haven't checked which one in details.
Explanation
Looking at the stack trace you can see this:
As you can see there is a call to
-[UICollectionViewAccessibility(SafeCategory) _dequeueReusableViewOfKind:withIdentifier:forIndexPath:]
that references something about Accessibility. So I went to Accessibility settings in Settings app, and I found out that my setting for Accessibility Shortcut was "switch control" instead of nothing. So I disabled it, I ran again the app and my stack trace now was fine:This is a known issue in iOS 7: http://openradar.appspot.com/15357491
Update 2:
It turns out that this answer is not correct. Please see the answer below or by clicking this link https://stackoverflow.com/a/20147799/814389 instead.
Update
So I have revisited this answer since as I have found out a little more about this bug..
I grabbed all the devices I had available to me and ran the same tests on each one and these were the results:
As you can see, it looks like for some reason cell reuse is not working on older iPads (ones that aren't capable of rendering the blurs).
I initially thought that Apple may have just prevented reuse on older iPads due to some sort of performance issue but if that was to have made sense, the iPhone 4 would also show the same results.
To get around this issue in my application, I have a NSMutableDictionary in my collectionViewController and I am storing my cells in there with the key being the indexPath.. In my case this is OK as I only have around 9 cells and their indexPaths never change but if you needed something more flexible then maybe it would be a good idea to checkout PSTCollectionView (https://github.com/steipete/PSTCollectionView)
Just tested this out on a physical device.. and it seems to work fine on both iOS 7 and 6 but not iOS 7 Simulator!!!
Just put some logs in the collectionView sample:
Then scrolled to the bottom on all three devices and this was the output:
iOS 7 Simulator
iOS 6 Device (iPhone 5)
iOS 7 Device (iPhone 5s)
You can tell that there have been some changes between reuse in iOS 6 and 7 because 1) It doesn't work in the simulator and 2) If you do a really fast scroll to begin with, the cells aren't initially ready for reuse so it has to create a new one to compensate where iOS 6 didn't (see my logs).
There goes half of my day trying to fix a bug that only happens on the simulator.