title: Two Cells In One UICollectionView Xcode
body: <p>[ I want to use two custom cells in one UICollectionView and this is by far I have been trying and I am stuck. Cell one for first and row and cell two of the second row and again cell one for 3rd, you get the point.</p>
  <p>here is the code ]</p>
  <pre><code>
  import UIKit
  
  class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
      
      @IBOutlet weak var CV: UICollectionView!
      
      let labelArray = [&quot;one&quot;, &quot;two&quot;, &quot;three&quot;, &quot;four&quot;]
      let imageArray = [UIImage(named: &quot;one&quot;), UIImage(named: &quot;two&quot;), UIImage(named: &quot;three&quot;), UIImage(named: &quot;four&quot;) ]
      override func viewDidLoad() {
          super.viewDidLoad()
          CV.delegate = self
          CV.dataSource = self
          CV.reloadData()
          
  //        let nibCellOne = UINib(nibName: &quot;CVCellOne&quot;, bundle: nil)
  //        let nibCellTwo = UINib(nibName: &quot;CVCellTwo&quot;, bundle: nil)
  //        CV.register(nibCellOne, forCellWithReuseIdentifier: &quot;CVCellOne&quot;)
  //        CV.register(nibCellTwo, forCellWithReuseIdentifier: &quot;CVCellTwo&quot;)
          CV.register(CollectionViewCell.self, forCellWithReuseIdentifier: &quot;CVCellOne&quot;)
          CV.register(CollectionViewCell.self, forCellWithReuseIdentifier: &quot;CVCellTwo&quot;)
      }
      func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -&gt; Int {
          return 4
      }
      
      func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -&gt; UICollectionViewCell {
          if indexPath.section == 0 {
              let cell1 = collectionView.dequeueReusableCell(withReuseIdentifier: &quot;CVCellOne&quot;, for: indexPath) as! CollectionViewCell
                  cell1.cellOneLabel.text = labelArray[indexPath.row]
                  return cell1
  
              }
              else if indexPath.section == 1 {
              let cell2 = collectionView.dequeueReusableCell(withReuseIdentifier: &quot;CVCellTwo&quot;, for: indexPath) as! CollectionViewCell
             
              cell2.cellTwoLabel.text = labelArray[indexPath.row]
              return cell2
          } else if indexPath.section == 2 {
              let cell1 = collectionView.dequeueReusableCell(withReuseIdentifier: &quot;CVCellOne&quot;, for: indexPath) as! CollectionViewCell
              cell1.cellOneImage.image = imageArray[indexPath.row]
              cell1.cellOneLabel.text = labelArray[indexPath.row]
              return cell1
          } else {
              let cell2 = collectionView.dequeueReusableCell(withReuseIdentifier: &quot;CVCellTwo&quot;, for: indexPath) as! CollectionViewCell
              cell2.cellTwoImage.image = imageArray[indexPath.row]
              cell2.cellTwoLabel.text = labelArray[indexPath.row]
              return cell2
          }
      }
  
  
  }
  
  </code></pre>
  <p><a href="https://i.stack.imgur.com/pa04K.jpg" rel="nofollow noreferrer">This is the customCell xib file</a></p>