Roi Mulia
Roi Mulia

Reputation: 5896

Check if UICollectionViewCell already created to avoid reuse

I'll make it short.

Situation

Let say i have 5 cells, im doing some kind of "method" on UIView who's placed inside the cell(as a subview). I need this "method" to occur only once(when the cell initialize),and avoid this "method" from happening again over the cellForItemAtIndexPath.

What i have tried

Any suggestions how to determine if the cell is already been created even when reusing cells?

Thanks guys!

Upvotes: 0

Views: 514

Answers (2)

matt
matt

Reputation: 534885

Okay, I finally understood what the question is!

This is actually really easy, because the list that keeps track of this already exists. It's the data model.

So, in cellForRowAtIndexPath:, you are looking into the data model for this section/row and using that info to populate the cell.

Well, in that data model, you also need to have a Bool value that starts out false, and (in cellForRow) if it's false you switch it to true and do your one-time configuration. In that way, you end up doing this task exactly once for each apparent row of the table.

Really this is exactly the same as when I populate cells with images that need to be downloaded from the Internet. How do I know whether I've downloaded the image for this row? Because the image is right there in the model! If it's not there, I know I need to start downloading it. If it is there, I've done this work already and I just supply the image. Thus the image for every row is downloaded a maximum of once.

Upvotes: 1

Juri Noga
Juri Noga

Reputation: 4391

It's not the best approach, but you can store cell's indexes, where method is already finished. And when cellForItemAtIndexPath you can check if current indexPath is contained in array. If you have only one section you can store just Int's.

Upvotes: 0

Related Questions