title: UITableViewCell loading incorrect image after reuse
body: <p>I have a tableview that populates its cells with data from firebase. Each cell posses an image and each image is loaded asynchronously and cached. The <strong>problem</strong> is that random cells will use the wrong image, although the other data in the cell is correct. If I scroll so that the cell goes off view, the correct image then loads. </p>
  
  <p><strong>Research</strong>:</p>
  
  <p><a href="https://stackoverflow.com/questions/16686516/images-in-uitableviewcells-are-loading-wrong">Images in UITableViewCells are loading wrong</a> (Dont really understand Obj C)</p>
  
  <p><a href="https://stackoverflow.com/questions/40773208/what-is-the-correct-way-to-use-prepareforreuse">What is the correct way to use prepareForReuse?</a></p>
  
  <p><strong>Image Cache:</strong></p>
  
  <pre><code>import Foundation
  import UIKit
  
  let imageCache = NSCache&lt;NSString, AnyObject&gt;()
  
  extension UIImageView {
  
      func loadImageUsingCache(urlString: String) {
          self.image = #imageLiteral(resourceName: "logo3")
  
          if let cachedImage = imageCache.object(forKey: urlString as NSString) as? UIImage {
              self.image = cachedImage
              return
          }
  
          let url = URL(string: urlString)
          URLSession.shared.dataTask(with: url!, completionHandler: {(data, response, error) in
              if error != nil {
                  print(error!)
                  return
              }
  
              DispatchQueue.main.async(execute: {
                  if let downloadedImage = UIImage(data: data!) {
  
                      imageCache.setObject(downloadedImage, forKey: urlString as NSString)
                      self.image = downloadedImage
                  }
              })
  
          }).resume()
  
  
  
      }
  
  }
  </code></pre>
  
  <p><strong>Tableview Cell</strong></p>
  
  <pre><code>import Foundation
  import Firebase
  
  class GroupCell: UITableViewCell {
  
      var group: Groups!
      var members = [String]()
  
  
      @IBOutlet weak var groupImage: UIImageView!
      @IBOutlet weak var groupName: UILabel!
      @IBOutlet weak var groupRep: UILabel!
      @IBOutlet weak var memberCount: UILabel!
  
      override func awakeFromNib() {
          super.awakeFromNib()
  
      }
  
  
      func configureCell(group: Groups) {
          self.group = group
  
          self.groupName.text = group.name
          self.groupRep.text = "\(group.groupScore)"
  
          if let groupImage = group.groupImage {
              self.groupImage.loadImageUsingCache(urlString: groupImage)
  
          } else {
              self.groupImage.image = //random image
          }
  
          for member in group.members {
              self.members.append(member.key)
  
          }
           self.memberCount.text = "\(members.count)"
      }
  }
  </code></pre>
  
  <p><strong>TableView</strong></p>
  
  <pre><code>func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&gt; UITableViewCell {
  
          if let cell = tableView.dequeueReusableCell(withIdentifier: "groupCell") as? GroupCell {
              let group = FriendSystem.system.activeGroups[indexPath.row]
              cell.configureCell(group: group)
  
              return cell
          }
  
          return UITableViewCell()
      }
  </code></pre>