Beginning Collection Views · Subclassing Collection View Layout | raywenderlich.com


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/6308-beginning-collection-views/lessons/18

Hi @bdmoakley, as much as I wanted to learn how the layoutAttributesForElements(in:) is used, the code is really verbose that i don’t know if i can recall this one in the future… :sweat_smile: is there an easier way to just have paddings around the cell? or any trick on how to make these concepts stick. :slight_smile:

Autolayout is one of those pain points that is incredibly frustrating to learn, but once you learn it, it is incredibly freeing to use - even with the verbosity. My suggestion is to take our autolayout course and really focus on autolayout in code.

Then, once you wrap your head around it - you’ll have no problem working with it directly, or using a third party library to take the pain out. I hope that helps!

@bdmoakley A few questions regarding this ‘layoutAttributesForElements’ method:

“We call layoutAttributesForElements on the super class to get the default attributes for the cells in a given rectangle”

  • What is this given rectangle you mention? What rectangle is defined? Where does this come from?

“We iterate through the collection of attributes and make sure the attributes are for a given cell by making sure representedElementKind == nil”

  • Why does this mean it is a cell and not something else? What is representedElementKind?

Why did you make a copy of of item when you wrote
let cellAttributes = item.copy() as! UICollectionViewLayoutAttributes ?

@bdmoakley Can you please help with this when you get a chance? Thank you - much appreciated! :]

Hi Chuck,

Here’s my breakdown. I hope this helps. This is kind of advanced usage of Collection Views so I completely understand your confusion.

Whenever you have questions about API specifics, it’s always a good starting place to check the documentation. This is from Apple’s documentation:

The rectangle (specified in the collection view’s coordinate system) containing the target views.

Essentially, we’re using it to get the bounding area for all the cells which we then apply the inset. We’re calling the parent class and we’re getting it from it.

We’re making sure that we are working with a cell. If the value is nil, then we aren’t dealing with a cell but with a supplementary view or a decoration view. representedElementKind returns a string for the type.

For example, if we were working with headers, we’d check that the representedElementKid
== UICollectionElementKindSectionHeader. But we’re working with cells, so we check for nil. Why nil? That’s how Apple designed the API. This is from the docs:

This property is nil if the representedElementCategory property contains the value UICollectionView.ElementCategory.cell.

I’m copying the contents to make new changes. If you don’t copy the attributes and make changes, you’ll get a warning. I’m guessing making changes messes with the internal state.

I hope this helps. Definitely reach out if you have any more questions. Cheers!

Thank you kindly for taking the time to clear all that up! I really enjoyed this course, but it was just this section that got fuzzy for me.

Also, thank you for pointing out the references in the documentation. I attempted to look there, but as I’m sure you can imagine for a beginner, it can be a little challenging finding the exact bit of information in there. I think this simply happens because I look up something I’m unfamiliar with, which links to something else I’m unfamiliar with, and so on. I’m sure this will happen less and less as I become more familiar with the various APIs.

Hi Chuck - Glad to help. And yes, the documentation can be certainly opaque at times. It’s can be bad when the docs aren’t up to date. Apple tends to be pretty good. Unity, not so much.

Typically, my flow when I have a question is to check the docs, make a hypothesis, and then do some experiments in code (by removing, adding or altering lines). After which, I’ll check with other people. But I’ve been doing this for awhile. As a beginner, definitely raise your hand if you have any issues and I (as well as the instructors) will be happy to help. :slight_smile:

Cheers!