Beginning Collection Views · Customize Collection Views | Ray Wenderlich


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

Hello,

I am working on project implementing it using customize UICollectionViewCell programmatically and I don’t use the story board , I would like to set the cell auotolayout when I call it In the UICollectionVewContriller because when I rotate the device back from horizontal , the cell doesn’t go back the vertical . could you please help me with that?

That’s really beyond the scope of the course. We assist with teaching the tutorials as opposed to working through issues on your own apps. You can post it in our forums to see what the community thinks but StackOverflow is a good place for this asssitance. Thanks!

Hello @bdmoakley

I figured out a better way to calculate the width and I’m sure you know of it too.
Perhaps you wanted to keep things simple but just in case other people are wondering here it is:

let numCols: CGFloat = 3
let layout = collectionView.collectionViewLayout as! UICollectionViewFlowLayout
let width = (view.frame.size.width - ((numCols - 1) * layout.minimumInteritemSpacing)) / numCols

Also I have 1 question:

Is there a reason why you did not write the calculation code in the viewDidLayoutSubviews method? I think it is a better approach since it allows to safely access the collection views frame size.

If you did this in order to keep things simple here’s my feedback to this decision:

If I see imperfect solutions in video courses I will not continue watching until I’ve found a better way to do it. I want to learn this stuff the right way and I’m sure I’m not alone. Please consider spending the extra time explaining these basic things. It is well worth it since it probably takes the students much longer finding the solution themselves.

Best Regards
Martin

@hettiger Thank you for sharing your solution - much appreciated! :]

1 Like

@bdmoakley, great class Brian! I am wondering why you think it is better to calculate a cell’s width from the view’s width as opposed to the collection view’s width. I think it would be more reliable to use the collection view’s width, because it won’t always occupy the entire view.

Let me know what you think. Thanks.

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

Hi Brian,

I was trying to use UICollectionViewCell in storyboard(instead of separate Nib file), But I want to register cell in ViewController, Would that be possible?
As registering with class is not possible because its initiating from storyboard nib and not class, and registering with nib is also not possible because UICollectionViewCell is in mainStoryBoard itself.
Please help…

@bdmoakley Do you have any feedback about this? Thank you - much appreciated! :]

Just wondering is ViewDidLoad is the right place to access view.frame.size.width ? Can the View frame change in ViewWillAppear and ViewDidAppear?

Hi Lancy, if you need to access the frame width, a good place to do it is in viewDidLayoutSubviews

hi, I know there is a newer topic, but I’d like to explore “the old way” before jumping to the newest one. I tried to follow the lessons, but even if I cut and copy the code from the resource project, I have all the cells on one row and half, horizontally, and small. here my https://github.com/stark226/test-collectionView-058945 using Xcode 12.0.1 on iPhone 11 simulator. the resource project works fine instead. thanks in advance for your help