kodeco.com Forums

Video Tutorial: Collection Views Part 1: Getting Started

In this video we look at the basics of Collection Views. We build a simple collection view displaying the index of each cell.


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

Hi Tim,

At 13m45s near the end you removed the minimum spacing using size inspector for the UICollectionView. I also like to set properties for UI elements in code, so looked it up in the UICollectionViewFlowLayout doc. Just need to add the following to the end of MasterViewController viewDidLoad:

layout.minimumInteritemSpacing = 0

Thanks for making the video, I can see this is going to be of much use for me.

Paul

Hi Tim, In the challenge solution there is some nesting of if let in the prepareForSegue. I had a go at flattening this with guard let :

  override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "MasterToDetail" {
      guard let indexPath = collectionView?.indexPathsForSelectedItems()?.first! else { return }
      guard let paper = papersDataSource.paperForItemAtIndexPath(indexPath) else { return }
      let detailViewController = segue.destinationViewController as! DetailViewController
      detailViewController.paper = paper
    }
  }

Appears to work fine, I’m not so experienced with doing this. Would appreciate the wisdom of others, if this looks good or not.

Paul

Hello Tim,

In this video, you set minimum spacing and all = 0. Then, could you explain me how did we get the white spacing in between the cells ?

It’s true that we set the minimum spacing between the cells themselves to be 0. That means that we really are butting them together with no space between them.

If you go back to around 6:30 in the video, you might recall that we centered a green UIView in the prototype cell. We also sized the UIView with AutoLayout constraints of 2 points (with Constrain to Margins unchecked.)

That makes the UIView smaller than the CollectionView cell by 4 points. So we end up with 2 points of white space around the views. That creates the white space that you see.

That looks good. You can sometimes gang the conditions together, on one line in the guard. That way you can create the deatilViewController only if the both of the conditions are met.

Try this:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { if segue.identifier == "MasterToDetail" { guard let indexPath = collectionView?.indexPathsForSelectedItems()?.first, paper = papersDataSource.paperForItemAtIndexPath(indexPath) else { return nil } let detailViewController = segue.destinationViewController as! DetailViewController detailViewController.paper = paper } }

Hi, there’s a bug in the Resources, on PapersDataSource.swift, line 142, it has:

if sections.contains(section) {
    sections.append(section)
}

And it should be:

if !sections.contains(section) {
    sections.append(section)
}

The error is fixed in the resources on Part 3, but in the resources up until then, it’s not…