1

I am downloading and showing thumbnails from server with AlamofireImage , the downloading works fine but when I scroll tableivew cell image changes all the time , I have searched and did not find any solution for example prepareForReuse in custom cell calls. Here is my code :

  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! TrendingCell

        let trendingIndex = trendingArray[indexPath.row] as! [String:Any]

        if let media = trendingIndex["thumbnails"] as? [String:Any] {
            if let thumbnail = media["medium"] as? [String:Any] {
                thumbnailURL = String(describing:thumbnail["url"]!)
            }
        }



        Alamofire.request(thumbnailURL).responseImage { response in
            if let image = response.result.value {
                cell.thumbnail.image = image
             }

        }

        return cell
    }

How to avoid image changing when user scrolls tableview ?

iOS.Lover
  • 5,923
  • 21
  • 90
  • 162

1 Answers1

1

As Salman Ghumsani mentioned here :

we can use AlamofireImage extension to set a default thumbnail like this :

  //Download and set thumbnail
        if let imageURL = URL(string: thumbnailURL), let placeholder = UIImage(named: "Default") {
            cell.thumbnail.af_setImage(withURL: imageURL, placeholderImage: placeholder) //set image automatically when download compelete.
        }

Now when you scroll table view images did not change.

iOS.Lover
  • 5,923
  • 21
  • 90
  • 162