title: UICollectionViewCell register class fails, but register nib works
body: <p>Creating a custom <code>UICollectionViewCell</code> for my <code>UICollectionViewController</code>, it works by registering the nib; however, fails to register by class.</p>
  
  <p><strong>Using registerClass() - fails</strong></p>
  
  <p>Registering by class seems correct - it builds, but throws an exception at runtime unwrapping optional elements from the UIView.  Without referencing outlets, it runs; however, no cells appear within the collection view.  </p>
  
  <pre><code>collectionView?.registerClass(MyCollectionViewCell.self, 
                                forCellWithReuseIdentifier: "myCell")
  </code></pre>
  
  <p>Clearly the view is not loaded; however, I'd think setting the custom class of the cell would provide necessary linkage.</p>
  
  <p><strong>Using registerNib() - works</strong></p>
  
  <p>Registering by nib works, which makes sense as it is explicitly loading the view.</p>
  
  <pre><code>let nib = UINib(nibName: "MyCollectionViewCell", bundle: nil)
  collectionView?.registerNib(nib, forCellWithReuseIdentifier: "myCell")
  </code></pre>
  
  <p>Here the view is explicitly referenced, and custom class is set for the view; however, something about this seems wrong.</p>
  
  <h1>Sample project</h1>
  
  <p>Isolating the issue in a sample project, below are views and code to replicate; or, this project is <a href="https://github.com/jasonsturges/UICollectionView-Test">available at GitHub</a>.</p>
  
  <p><strong>Main.storyboard</strong></p>
  
  <p>My main storyboard initial view controller is a <code>UICollectionViewController</code> subclassed as <code>MyCollectionViewController</code>:</p>
  
  <p><a href="https://i.stack.imgur.com/pDFDg.png"><img src="https://i.stack.imgur.com/pDFDg.png" alt="UICollectionViewController"></a></p>
  
  <p><strong>MyCollectionViewController.swift</strong></p>
  
  <p>Showing both registering by nib, and by class:</p>
  
  <pre><code>import UIKit
  
  class MyCollectionViewController: UICollectionViewController {
  
      var data = [String]()
  
      override func viewDidLoad() {
          super.viewDidLoad()
  
          data = ["a", "b", "c"]
  
          // This works, by nib
          let nib = UINib(nibName: "MyCollectionViewCell", bundle: nil)
          collectionView?.registerNib(nib, forCellWithReuseIdentifier: "myCell")
  
          // This fails, by class
          //collectionView?.registerClass(MyCollectionViewCell.self, forCellWithReuseIdentifier: "myCell")
      }
  
      override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -&gt; Int {
          return data.count
      }
  
      override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -&gt; UICollectionViewCell {
          let cell = collectionView.dequeueReusableCellWithReuseIdentifier("myCell", forIndexPath: indexPath) as! MyCollectionViewCell
  
          cell.title.text = data[indexPath.row]
  
          return cell
      }
  
  }
  </code></pre>
  
  <p><strong>MyCollectionViewCell.xib</strong> </p>
  
  <p>View is a <code>UICollectionViewCell</code> subclassed as <code>MyCollectionViewCell</code> with a <code>UILabel</code>:</p>
  
  <p><a href="https://i.stack.imgur.com/aRbyU.png"><img src="https://i.stack.imgur.com/aRbyU.png" alt="MyCollectionViewCell"></a></p>
  
  <p>In my nib the <em>Collection Reusable View Identifier</em> has been set to: <code>myCell</code>:</p>
  
  <p><a href="https://i.stack.imgur.com/mBEtV.png"><img src="https://i.stack.imgur.com/mBEtV.png" alt="MyCollectionViewCell-identifier"></a></p>
  
  <p><strong>MyCollectionViewCell.swift</strong></p>
  
  <p>Defines the class, and has an <code>IBOutlet</code> to the label:</p>
  
  <pre><code>import UIKit
  
  class MyCollectionViewCell: UICollectionViewCell {
  
      @IBOutlet weak var title: UILabel!
  
  }
  </code></pre>
  
  <p>Using the nib, execution appears as:</p>
  
  <p><a href="https://i.stack.imgur.com/4rt8u.png"><img src="https://i.stack.imgur.com/4rt8u.png" alt="iphone-4s"></a></p>
  
  <p>Why can't I register <code>UICollectionViewCell</code> by class?</p>
  
  <p>As well, I'm a little fuzzy as to whether the prototype cell needs to remain on the main storyboard collection view controller.  There, I have not defined any reusable view identifier.</p>