2

EDIT: seems like same question as this
I made collection view with custom layout and cell but my cell's positions were a little off so I decided to number them so that I could debug it easier. I followed this guy's answer to number my cell using labels. Somehow myLabel was nil and caused my app to crash.

I believe I have connected the outlets correctly but maybe someone could provide a list for me to check if I am doing anything wrong.

ViewController

import UIKit

class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {

  @IBOutlet var gridView: UICollectionView!

  private let reuseIdentifier = "DesignCell"
  private let numberOfSections = 1
  private let numberOfCircles = 48

  override func viewDidLoad() {
    super.viewDidLoad()
    self.gridView.registerClass(MyCell.self, forCellWithReuseIdentifier: reuseIdentifier)
  }

  func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
    return numberOfSections
  }

  func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return numberOfCircles
  }

  func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! MyCell

    cell.myLabel.text = String(indexPath.item) // myLabel is nil and causes a crash
    return cell
  }

  override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
  }

}

GridLayout (my custom layout)

import UIKit
import Darwin

class GridLayout: UICollectionViewLayout {

  private let numberOfColumns = 12
  private let numberOfRows = 4

  private var cache = [UICollectionViewLayoutAttributes]()
  private var contentHeight: CGFloat = 0
  private var contentWidth: CGFloat {
    let insets = collectionView!.contentInset
    return CGRectGetWidth(collectionView!.bounds) - insets.left - insets.right
  }

  override func prepareLayout() {
    // some calculation i did for my cells
  }

  override func collectionViewContentSize() -> CGSize {
    return CGSize(width: contentWidth, height: contentHeight)
  }

  override func layoutAttributesForElementsInRect(rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
    var layoutAttributes = [UICollectionViewLayoutAttributes]()

    for attr in cache {
      if CGRectIntersectsRect(attr.frame, rect) {
        layoutAttributes.append(attr)
      }
    }
    return layoutAttributes
  }
}

MyCell

import UIKit

class MyCell : UICollectionViewCell {

  @IBOutlet weak var myLabel: UILabel!

}
Community
  • 1
  • 1
Gerald
  • 516
  • 1
  • 8
  • 16

1 Answers1

5

Try to remove this line within viewDidLoad() in your ViewController class:

self.gridView.registerClass(MyCell.self, forCellWithReuseIdentifier: reuseIdentifier)

It's not needed since you have created your UICollectionView in Storyboard, connected to your dataSource and delegate, and you have added all the required methods:

  • numberOfItemsInSection
  • cellForItemAtIndexPath
Roel Koops
  • 800
  • 2
  • 16
  • 26
  • it works, but I am a little puzzled. why does registering the class again cause this problem? – Gerald Feb 06 '16 at 13:20
  • 1
    See this [link](http://www.techotopia.com/index.php/An_Overview_of_iOS_6_Collection_View_and_Flow_Layout). If your collectionView cell is created with nib use registerNib: If it's written in code use registerClass: – Roel Koops Feb 06 '16 at 13:42