I have a UICollectionView
with a lot of cells. I want to load all the cells in viewDidLoad
, but UICollectionView
loads its cells only when they are visible.
Any suggestions?
To restate the problem, the collection view cells contain images that are loaded lazily, but you require that they appear immediately. The idea behind your question is to force the collection view to preload all of it's cells. But you don't really want the cells, you want the images, so let's get the images.
The question doesn't contain much detail about where the images are, or the model, or your method of loading them in the datasource method, so here's an outline of the advice (which I can edit to be more specific if you post more specifics):
For local images, instantiated via imageNamed:
, create a dictionary whose keys are the image names, and whose values are the instantiated images, build this dictionary before the collection view displays, then use the dictionary in the datasource method.
For remote images, your collection view datasource must fetch these asynchronously. I gave an answer here that covers this in detail.
In any case, @CrimsonChris was right to be skeptical about pre-loading the cells. His answer, in my opinion, is ill-advised. (I think he thinks so, too, but was trying to offer an answer to the question as written).
Create the cells in viewDidLoad
and put them in an array or dictionary. Then return the right one in cellForItemAtIndexPath:
. This approach will NOT leverage cell reuse and may use a large amount of memory. However, it may scroll slightly smoother because the cells don't need to be setup on scroll.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With