title: Replacing default images in a collection view with user selected photos- swift
body: <p>So I have a collection view that populates three default images on each cell. The user can select images through the new PHPicker, what I want to accomplish is</p>
  <ol>
  <li>to replace default images with selected photos, so the first selected image should replace the camera cell and so on ... (see images through links at the bottom)</li>
  <li>to display the default images in case user deletes the selected images</li>
  </ol>
  <p>currently when I send a new photo to the imgArray it gets displayed in a new cell before the camera cell, as I'm using insert method like so: imgArray.insert(image, at: 0).</p>
  <p>My code:</p>
  <pre><code>  var imgArray = [UIImage(systemName: &quot;camera&quot;), UIImage(systemName: &quot;photo&quot;), UIImage(systemName: &quot;photo&quot;)]
  
     func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -&gt; UICollectionViewCell {
  
      let cell = collectionView.dequeueReusableCell(withReuseIdentifier: &quot;1&quot;, for: indexPath) as! CustomCell
      cell.imageView.image = imgArray[indexPath.row]
      cell.imageView.tintColor = .gray
      cell.imageView.layer.cornerRadius = 4
      return cell
  }
  
   func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {
      dismiss(animated: true, completion: nil)
      
      for item in results {
          
          item.itemProvider.loadObject(ofClass: UIImage.self) { image, error in
  
              if let image = image as? UIImage {
                  DispatchQueue.main.async {
                      self.imgArray.insert(image, at: 0)
                      self.collectionView.reloadData()
                  }
              }
          }
      }
  }
  </code></pre>
  <p>I have tried to remove the first item of the array, and then insert new photo like this:</p>
  <pre><code>                    self.imgArray.removeFirst(1)
                      self.imgArray.insert(image, at: 0)
                      self.collectionView.reloadData()
  </code></pre>
  <p>but this would work just one time as the code itself says, after that all replacement takes place just in the first cell.
  So how can I get to other cells after first replacement? Any other approach that gives the same result will help me alot. Thanks in advance guys!</p>
  <p><a href="https://i.stack.imgur.com/DpPPe.png" rel="nofollow noreferrer">before selection</a></p>
  <p><a href="https://i.stack.imgur.com/DkIJj.png" rel="nofollow noreferrer">after selecting three images</a></p>