title: Swift Loading Images in a UITableView Asynchronously - top cells have no picture
body: <p>Right now I am using Google Places API to download a list of places radially near the user. When I get the JSON response, I parse it and make an array of places which is used to populate the table view. Previously, I loaded the image synchronously which made the load time for the tableview far too long. I've since added an asynchronous load, however, the problem occurs where the table view loads, but the cells that are initially shown do not refresh after the download of the image completes, only when I scroll passed them and then back up, does the image display (all places below the first few visible have loaded pictures). If there is a way to refresh the tableView inside the async call of my places object, that would probably help it, but I'm not sure if reloading a tableView that many times is good practice or if there is a more cleanly way.</p>
  
  <p>I'm relatively new to iOS development, and so if anyone has a different approach to how I am doing this, I would love to hear it.</p>
  
  <p>Getting the Places into an array format:</p>
  
  <pre><code>let task = URLSession.shared.dataTask(with: url!) {(data, response, error) in
  do{
      print("\n Starting JSON Parsing \n")
      if let json = try JSONSerialization.jsonObject(with: data!) as? AnyObject{
          if let results = json["results"] as? NSArray{
              print("\(results.count)")
              for case let result as NSDictionary in results{
                  let item = result
                  let name = item["name"]
  
                  if let name = name,
                      let id = item["id"],
                      let photos = item["photos"] as? NSArray,
                      let photo = photos.firstObject as? NSDictionary,
                      let photoRef = photo["photo_reference"] {
                      let place = Place(name: "\(name)", desc: "\(id)", imageRef: "\(photoRef)")
                      self.placeList.append(place)
                  }
                  else {
                      print("\n\n \(name) \n\n")
                  }
              }
              self.placeList.sort(by: {$0.name &gt; $1.name})
  
              DispatchQueue.main.async {
                  self.tableView.reloadData()
              }
          }
      }
  }
  catch {
      print("Error deserializing JSON: \(error)")
  }
  </code></pre>
  
  <p>Places object initializer:</p>
  
  <pre><code>    init(name: String, desc: String, imageRef: String) {
          self._desc = desc
          self._imageURL = imageURL
          self._name = name
  
          if let url = NSURL(string: "https://maps.googleapis.com/maps/api/place/photo?maxwidth=150&amp;photoreference=\(imageRef)&amp;key={MyKey}") {
              DispatchQueue.global().async {
                  if let data = NSData(contentsOf: url as URL) {
                      DispatchQueue.main.async {
                          self.image = UIImage(data: data as Data)
                      }
                  }
              }
          }
  </code></pre>