Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load all cells of UICollectionView at once?

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?

like image 437
StrawHara Avatar asked Jun 20 '14 13:06

StrawHara


2 Answers

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).

like image 87
danh Avatar answered Oct 27 '22 01:10

danh


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.

like image 41
CrimsonChris Avatar answered Oct 27 '22 02:10

CrimsonChris