DEV Community

How do I create a UICollectionView with standard layout programmatically in Swift 4?

Daniel P-G on February 19, 2019

Hello all! I'm pretty new to iOS development and have been mostly working with Interface Builder. However, I've recently started learning how to c...
Collapse
 
recursivefaults profile image
Ryan Latta

It's been a while since I've done this. I'll try to help.

1) I haven't seen that syntax before, but its allowing you to set properties for initialization using a closure/function.

2) I'm not sure how many collectionviewlayouts exist, but even if there is a default, you'll need to be aware of it. This is a case where IB is attempts to make things less complicated by removing your need to know all these little things while allowing you to override or customize

3) Ah the magic. Ok, knowing this part (And this is something to nail for interviews) is key.

CollectionViews and TableViews use a pattern called the Flyweight pattern. Essentially this pattern recycles objects. So your CollectionView has CollectionViewCells. Under the covers it has a small pool of them and it continually recycles them as they scroll on and off the screen. All of those collectionview methods you have to implement as a part of the datasource and vc delegate orchestrate this.

In your initializer you registered a specific cell with the collection view controller. That's basically telling the collectionview to get a few reay for recycling. You also gave it an identifier, "cell" this is how you'll get access to one of those recycled cells. You could register different kinds of cells using this mechanism.

Then there is a method called dequecellwithreuseidentifier. This is the magic. This method gets called as the need to show another cell comes up. You then ask it to give you a recycled cell based on the reuse identifier. iOS obliges by giving you one. At this point it still has everything it had the last time it was viewed.

Now, in that method (There is a more performant one to use called willDisplayCell [I think]) you can modify that cell. So your question about changing a title and whatnot happens here.

If your cell doesn't have those elements then too bad. If you built a custom cell, for instance, you'd deque the cell and maybe cast it back to the correct type so you can access its elements. The default cell, depending on how you've set it up will have titles and whatnot. So where you're setting the background to white, change the title to something else there too.

Collapse
 
danpgomez profile image
Daniel P-G

Hi @recursivefaults thank you so much for such a detailed response! I really appreciate it 😀. Is there a more common or better syntax for creating a collection view than the one I mentioned here? How would you do it?

  let myCollectionView: UICollectionView = {}()
Collapse
 
recursivefaults profile image
Ryan Latta

I think you're two choices are going to be a classic version and the on you used. The classic one would look like this.

let myCollectionView: UICollectionView = UICollectionView()

I'm assuming there aren't required parameters.

Generally speaking I don't use closures for things when I can make it more explicit otherwise. I find they read poorly and depending on the language and situation you have to know that the closure executes on a seperate thread or execution context.

I've not used the closure based way before, so I can't speak to its long term usefulness. My first reaction to it is that I'd probably not use it unless I was doing something very complex that wrapping it up a function wouldn't cover.

Thread Thread
 
danpgomez profile image
Daniel P-G

Thank you so much!