title: UITableViewCell image loading wrong images
body: <p>For now i download the image, story it in a mutable dictionary and then verify if the image was already downloaded and if not, download it and store it. As a key i use the <code>indexPath</code>.</p>
  
  <p>This code kinda works, but from the tests i did if i scroll too fast the cell image will load the wrong one and after a split of a second it will load the right one (replacing the wrong image).</p>
  
  <p>Im always clearing my thumbnail (imageView) after i call the method so i don't know why im getting this bug.</p>
  
  <p>I though that maybe the <code>if(self.imageCache.object(forKey: cacheKey) != nil)</code> statement was true and thats why i would get multiple images, but the breakpoint didn't stop at once when i was scrolling down.</p>
  
  <p>Any ideas?</p>
  
  <pre><code>  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&gt; UITableViewCell 
    {
  
          let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! MovieCellController
          cell.thumbnail.image = UIImage()
  
          let cacheKey = indexPath.row
  
          if(self.imageCache.object(forKey: cacheKey) != nil)
          {
              cell.thumbnail.image = self.imageCache.object(forKey: cacheKey) as? UIImage
          }
          else
          {
              DispatchQueue.global(qos: DispatchQoS.QoSClass.default).async {
                  if let url = NSURL(string:  self.moviesCollection[indexPath.row].imageLink) {
                      if let data = NSData(contentsOf: url as URL) {
                          let image: UIImage = UIImage(data: data as Data)!
                          self.imageCache.setObject(image, forKey: cacheKey as NSCopying)
                          DispatchQueue.main.async(execute: {
                              cell.thumbnail.image = image
                          })
                      }
                  }
              }
          }
  
          cell.name.text = moviesCollection[indexPath.row].name
  
          return cell
  
  }
  </code></pre>