title: UITableViewCell asynchronously loading images issue - Swift
body: <p>In my app, I built my own asynchronous image loading class. I pass in a object, then it checks if the cache (NSCache) has the image, if not it will then check the file system if the image is saved already. If the image is not saved already, it will then download the image in the background (NSOperations help).</p>
  
  <p>This works great so far, but I have ran into a few small issues with the table view loading the images.</p>
  
  <p>First off, this is the function I use to set up the table view cell from<code>tableView(tableView:, willDisplayCell:, forRowAtIndexPath:)</code></p>
  
  <pre><code>func configureCell(cell: ShowTableViewCell, indexPath: NSIndexPath) {
  
      // Configure cell
      if let show = dataSource.showFromIndexPath(indexPath) {
  
          ImageManager.sharedManager.getImageForShow(show, completionHandler: { (image) -&gt; Void in
              if self.indexPathsForFadedInImages.indexOf(indexPath) == nil {
                  self.indexPathsForFadedInImages.append(indexPath)
  
                  if let fetchCell = self.tableView.cellForRowAtIndexPath(indexPath) as? ShowTableViewCell {
                      func fadeInImage() {
                          // Fade in image
                          fetchCell.backgroundImageView!.alpha = 0.0
                          fetchCell.backgroundImage = image
                          UIView.animateWithDuration(showImageAnimationSpeed, animations: { () -&gt; Void in
                              fetchCell.backgroundImageView!.alpha = 1.0
                          })
                      }
  
                      if #available(iOS 9, *) {
                          if NSProcessInfo.processInfo().lowPowerModeEnabled {
                              fetchCell.backgroundImage = image
                          }
                          else {
                              fadeInImage()
                          }
                      }
                      else {
                          fadeInImage()
                      }
                  }
                  else {
                      // Issues are here
                  }
              }
              else {
                  // Set image
                  cell.backgroundImage = image
              }
          })
  ...
  }
  </code></pre>
  
  <p>Where "// Issues are here" comment is, that is where I run into multiple issues.</p>
  
  <p>So far, I have not figured out another way to validate that the image belongs to the cell for sure where "// Issues are here" is. If I add</p>
  
  <p>cell.backgroundImage = image</p>
  
  <p>there, then it fixes the issue where sometimes the image will not display on the table view cell. So far the only cause I have found for this is that the image is being returned faster than I can return the table view cell so that is why the table view says there is not a cell at that index path.</p>
  
  <p>But if I add that code there, then I run into another issue! Cells will display the wrong images and then it lags down the app and the image will constantly switch, or even just stay on the wrong image.</p>
  
  <p>I have checked that it runs on the main thread, image downloading and caching is all fine. It just has to do that the table is saying there is no cell at that index path, and I have tried getting an indexPath for the cell which returns also nil.</p>
  
  <p>A semi-solution to this problem is called tableView.reloadData() in viewWillAppear/viewDidAppear. This will fix the issue, but then I lose the animation for table view cells on screen. </p>
  
  <p>EDIT:</p>
  
  <p>If I pass the image view into getImageForShow() and set it directly it will fix this issue, but that is less ideal design of code. The image view obviously exists, the cell exists, but for some reason it doesn't want to work every time. </p>