title: How to load custom cell ( xib) in UICollectionView cell using swift
body: <p>I have created a small sample project using Swift. I have created an "MyCustomView" as xib which contains label, button and imageView as shown in below code:</p>
  
  <pre><code>import UIKit
  
  @IBDesignable class MyCustomView: UIView {
  
      @IBOutlet weak var lblName: UILabel!
      @IBOutlet weak var btnClick: UIButton!
      @IBOutlet weak var myImageView: UIImageView!
  
      var view:UIView!
  
      @IBInspectable
      var mytitleLabelText: String? {
          get {
              return lblName.text
          }
          set(mytitleLabelText) {
              lblName.text = mytitleLabelText
          }
      }
  
      @IBInspectable
      var myCustomImage:UIImage? {
          get {
              return myImageView.image
          }
          set(myCustomImage) {
              myImageView.image = myCustomImage
          }
      }
  
  
      override init(frame : CGRect)
      {
          super.init(frame: frame)
          xibSetup()
      }
  
      required init?(coder aDecoder: NSCoder) {
          super.init(coder: aDecoder)
          xibSetup()
      }
  
  
      func xibSetup()
      {
          view = loadViewFromNib()
          view.frame = self.bounds
  
          // not sure about this ?
          view.autoresizingMask = [UIViewAutoresizing.FlexibleWidth, UIViewAutoresizing.FlexibleHeight]
          addSubview(view)
      }
  
  
      func loadViewFromNib() -&gt; UIView {
  
          let bundle = NSBundle(forClass: self.dynamicType)
          let nib = UINib(nibName: "MyCustomView", bundle: bundle)
          let view = nib.instantiateWithOwner(self, options: nil)[0] as! UIView
  
          return view
      }
  }
  </code></pre>
  
  <p>Attached the image of xib for the reference.
  <a href="https://i.stack.imgur.com/HFOmb.png" rel="noreferrer"><img src="https://i.stack.imgur.com/HFOmb.png" alt="Screenshot"></a></p>
  
  <p>In StoryBoard -> ViewController added UIViewCollection which as shown in the below image. In this viewcollection, I need that orange color cell to contain my custom xib to be loaded at runtime. </p>
  
  <p>How do I achieve this?</p>
  
  <p><a href="https://i.stack.imgur.com/QGYhN.png" rel="noreferrer"><img src="https://i.stack.imgur.com/QGYhN.png" alt="enter image description here"></a></p>
  
  <p>New Modified code as suggested by Sandeep</p>
  
  <p>// 1
      import UIKit</p>
  
  <pre><code>class ViewController: UIViewController {
  
      @IBOutlet weak var collectionView: UICollectionView!
  
      override func viewDidLoad() {
          super.viewDidLoad()
  
          self.collectionView.register(UINib(nibName: "MyCustomView", bundle: nil), forCellWithReuseIdentifier: "myCell")
  
      }
  
      override func didReceiveMemoryWarning() {
          super.didReceiveMemoryWarning()
          // Dispose of any resources that can be recreated.
      }
  
      func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -&gt; Int {
          return 7
      }
  
      func numberOfSectionsInCollectionView(collectionView: UICollectionView) -&gt; Int {
          return 1
      }
  
      func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -&gt; UICollectionViewCell {
  
          let cell : MyCustomView = collectionView.dequeueReusableCellWithReuseIdentifier("your_reusable_identifier", forIndexPath: indexPath) as! MyCustomView
  
          cell.lblName.text = "MyNewName"
          return cell
      }
  }
  </code></pre>
  
  <p>// 2
      import UIKit</p>
  
  <pre><code>@IBDesignable class MyCustomView: UICollectionViewCell {
  
      @IBOutlet weak var lblName: UILabel!
      @IBOutlet weak var btnClick: UIButton!
      @IBOutlet weak var myImageView: UIImageView!
  
      var view:UIView!
  
      @IBInspectable
      var mytitleLabelText: String? {
          get {
              return lblName.text
          }
          set(mytitleLabelText) {
              lblName.text = mytitleLabelText
          }
      }
  
      @IBInspectable
      var myCustomImage:UIImage? {
          get {
              return myImageView.image
          }
          set(myCustomImage) {
              myImageView.image = myCustomImage
          }
      }
  
  }
  </code></pre>