title: How to create UICollectionViewCell programmatically
body: <p>I'm trying to create <code>UICollectionView</code> programatically.
  I need to add labels inside the cells, so I Created <code>CollectionViewCell</code> class.</p>
  
  <p>This is the class:</p>
  
  <pre><code>import UIKit
  
  class MyCollectionViewCell: UICollectionViewCell {
  
      override init(frame: CGRect) {
          super.init(frame: frame)
      }
  
      required init?(coder aDecoder: NSCoder) {
          fatalError("init(coder:) has not been implemented")
      }
  }
  </code></pre>
  
  <p>And this is the collectionView implementation class:</p>
  
  <pre><code>import UIKit
  
  class TwoViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, UICollectionViewDelegate {
  
      let leftAndRightPaddings: CGFloat = 80.0
      let numberOfItemsPerRow: CGFloat = 7.0
      let screenSize: CGRect = UIScreen.main.bounds
      private let cellReuseIdentifier = "collectionCell"
      var items = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31"]
  
      override func viewDidLoad() {
          super.viewDidLoad()
          let flowLayout = UICollectionViewFlowLayout()
          let collectionView = UICollectionView(frame: self.view.bounds, collectionViewLayout: flowLayout)
          collectionView.register(MyCollectionViewCell.self, forCellWithReuseIdentifier: cellReuseIdentifier)
  
          collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "collectionCell")
          collectionView.delegate = self
          collectionView.dataSource = self
          collectionView.backgroundColor = UIColor.cyan
  
          self.view.addSubview(collectionView)
      }
  
      func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -&gt; Int
      {
          return self.items.count
      }
  
      func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -&gt; UICollectionViewCell
      {
          let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellReuseIdentifier, for: indexPath) as! MyCollectionViewCell
          cell.backgroundColor = UIColor.green
          return cell
      }
  
      func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -&gt; CGSize
      {
          let width = (screenSize.width-leftAndRightPaddings)/numberOfItemsPerRow
          return CGSize(width: width, height: width)
      }
  
      func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -&gt; UIEdgeInsets
      {
          return UIEdgeInsets(top: 20, left: 8, bottom: 5, right: 8)
      }        
  }
  </code></pre>
  
  <p>The error happens when the cell produced:</p>
  
  <pre><code>func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -&gt; UICollectionViewCell
  {
      let cell = collectionView.dequeueReusableCellWithReuseIdentifier(cellReuseIdentifier, forIndexPath: indexPath) as! MyCollectionViewCell
  </code></pre>
  
  <p>The error is:</p>
  
  <pre><code>Could not cast value of type 'UICollectionViewCell' (0x1033cc820) to 'CollectionViewProgramatically.MyCollectionViewCell' (0x1015a4f88).
  </code></pre>