title: How to NOT reuse cells in UICollectionView - Swift
body: <p>I have UICollectionView in a cell of UITableView. This UICollectionView displays user's profile photos. There are only a few items there (let's assume 10 photos is a maximum). </p>
  
  <p>So, I do not want to reload images from backend every time cell reusing. It's inconvenient and unfriendly UX. Therefore, I want to create 10 cells, load images into them and show. </p>
  
  <p>But I don't know how to create (instead of dequeuing) new custom cells within "cellForItem" method of UICollectionView. Or how to forbid reusing.</p>
  
  <p>I'm a kind of newbie in programming, so I would be glad if you explained me that in simple words and most detailed. Thank you. </p>
  
  <p>Here is my code:</p>
  
  <p>Setting CollectinView (uploading random local photos in testing purpose):</p>
  
  <pre><code>extension PhotosTableViewCell: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
  
      func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -&gt; Int {
          return 10
      }
  
      func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -&gt; UICollectionViewCell {
  
          if indexPath.row == 0 {
              let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "AddPhotoCollectionViewCell", for: indexPath)
              return cell
          } else {
              var cell = collectionView.dequeueReusableCell(withReuseIdentifier: "PhotoCollectionViewCell", for: indexPath) as! PhotoCollectionViewCell
              cell.photo = UIImage(named: "Photo\(Int.random(in: 1...8))") ?? UIImage(named: "defaultPhoto")
              cell.layer.cornerRadius = 10
  
              return cell
          }
      }
  
      func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -&gt; CGSize {
  
          let height = collectionView.frame.height
          let width = (height / 16.0) * 10.0
  
          return CGSize(width: width, height: height)
      }
  
  }
  </code></pre>
  
  <p>My custom PhotoCollectionViewCell:</p>
  
  <pre><code>class PhotoCollectionViewCell: UICollectionViewCell {
  
      @IBOutlet weak var imageView: UIImageView!
      @IBOutlet weak var heightConstraint: NSLayoutConstraint!
      @IBOutlet weak var widthConstraint: NSLayoutConstraint!
  
      var photo: UIImage! {
          didSet {
              // simulate networking delay
              perform(#selector(setImageView), with: nil, afterDelay: Double.random(in: 0.2...1))
  
              // setImageView()
          }
      }
  
      override func awakeFromNib() {
          super.awakeFromNib()
          // Initialization code
      }
  
      @objc private func setImageView() {
          let cellHeight = self.frame.height
          let cellWidth = self.frame.width
          let cellRatio = cellHeight / cellWidth
  
          let photoRatio = photo.size.height / photo.size.width
  
          var estimatedWidth = cellWidth
          var estimatedHeight = cellHeight
  
          if photoRatio &gt; cellRatio {
              estimatedWidth = cellWidth
              estimatedHeight = cellWidth * photoRatio
          } else {
              estimatedHeight = cellHeight
              estimatedWidth = cellHeight / photoRatio
          }
  
          widthConstraint.constant = estimatedWidth
          heightConstraint.constant = estimatedHeight
          imageView.image = photo
      }
  
      override func prepareForReuse() {
          imageView.image = nil
      }
  
  }
  </code></pre>