title: Populating Collection View from Array in Document Directory
body: <p>This is a follow-up from this <a href="https://stackoverflow.com/questions/48143229/accessing-userdefault-array-url-to-populate-collectionview">question</a>. Thanks so much to @rmaddy and @LeoDabus for your help thus far!</p>
  
  <p><strong>The Good</strong>: my addImage button gets me the correct image in collectionView the first time I pick an image. </p>
  
  <p><strong>The Bad</strong>: the second time I select an image, it changes both cells to the 2nd image. By the third/fourth/etc times I try to addImage, the image is always the same across each cell no matter what image I choose (i.e. 1st try: image1, 2nd try: image 2, image 2, 3rd try: image 2, image 2, image 2). </p>
  
  <p><strong>My question</strong>: How do I make the image chosen populate the incremental cells correctly? </p>
  
  <p>Here is my code:</p>
  
  <p>Get the number of cells in the collectionView from count</p>
  
  <pre><code>func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -&gt; Int {
  
      let fileManager = FileManager.default
      let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as String
      let dirContents = try? fileManager.contentsOfDirectory(atPath: documentsPath)
      let count = dirContents?.count
      return count!
          }
  </code></pre>
  
  <p>Populate the views</p>
  
  <pre><code>func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -&gt; UICollectionViewCell {
  
      let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! myCell
      let fileManager = FileManager.default
      let documentDirectory = fileManager.urls(for: .documentDirectory, in: .userDomainMask)[0]
      let directoryContents = try! fileManager.contentsOfDirectory(at: documentDirectory, includingPropertiesForKeys: nil)
  
      for imageURL in directoryContents where imageURL.pathExtension == "jpeg" {
          if let image = UIImage(contentsOfFile: imageURL.path) {
              cell.myImageView.image = image //[indexPath.row] **need "image" to be an array so can assign to indexPath.row
          } else {
              fatalError("Can't create image from file \(imageURL)")
          }
      }
      return cell
  }
  </code></pre>
  
  <p>addMyImage button opens the imagePicker    </p>
  
  <pre><code>@IBAction func addMyImage(_ sender: UIButton) {
  
  //imagePickerController stuff
  }
  </code></pre>
  
  <p>Save the imageURL in File Manager</p>
  
  <pre><code>func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
  
  if let imageURL = info[UIImagePickerControllerImageURL] as? URL {
  
      let documentDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
      do {
          try FileManager.default.moveItem(at: imageURL.standardizedFileURL, to: documentDirectory.appendingPathComponent(imageURL.lastPathComponent))
          collectionView.reloadData()
      } catch {
          print(error)
      }
  }
  </code></pre>
  
  <p>After lots of googling, I think the problem is I haven't assigned [indexPath.row] to an array in my cellForItemAt IndexPath, so while the documentDirectory gets updated with the URL, each new cell is not being pointed to the correct URL. Unfortunately, I'm calling a specific URL in that function</p>
  
  <pre><code>for imageURL in directoryContents where imageURL.pathExtension == "jpeg" 
  </code></pre>
  
  <p>so I am not sure how to assign the indexPath.row...</p>
  
  <p>Would appreciate any help!</p>