When I'm populating new data in my tableView Controller, my top cell is being rewrite by the bottom one. Let me explain. I have one image that is loading async in the latest cell row, in the bottom. All the other cells are loading static with an image that is in the app. When I scroll down my app and display my bottom cell which have a different image than the others cells, and I scroll up again, I see the first shop image has changed to the dynamic one loaded in the bottom cell. Does anyone know why this is happening?
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell : shopsTableViewCell = tableView.dequeueReusableCellWithIdentifier("shopCell", forIndexPath: indexPath) as! shopsTableViewCell
cell.index = indexPath.row
let qualityOfServiceClass = QOS_CLASS_BACKGROUND
let backgroundQueue = dispatch_get_global_queue(qualityOfServiceClass, 0)
let apiCall = webApi()
dispatch_async(backgroundQueue, {
apiCall.downloadPhotoAsync("http://api-ytyg.wbbdev.com/files/shops/\(shops[indexPath.row].id)/\(shops[indexPath.row].featured_img)"){(image: UIImage?) -> Void in
dispatch_async(dispatch_get_main_queue()){
if(image != nil){
if (cell.index == indexPath.row){
// shops[indexPath.row].photo = image!
cell.shopImg?.image = image
}
}else{
shops[indexPath.row].photo = UIImage.init(named: "No_Image_Available.jpg")!
}
}
}
})
cell.shopName?.text = shops[indexPath.row].name
cell.shopDescription?.text = shops[indexPath.row].address
cell.label1?.text = shops[indexPath.row].city + " | " + shops[indexPath.row].distance + "Km"
cell.becomeFirstResponder()
return cell
}