Boocha
Boocha

Reputation: 9

UITableView not updating images downloaded using Kingfisher

I am using Kingfisher to download and set images to imageviews in cells in UITableView. Everything works fine and the tableview displays the placeholder images first before the new images are downloaded. However these placeholder images are updated to correct images only after I scroll the table view. How do I update the cells immediately after the images are downloaded? Thank you.

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

    let cell = articlesTableView.dequeueReusableCell(withIdentifier: "articleCell", for: indexPath) as! ArticleCell

    let resource = self.articlesArray?[indexPath.item].downloadedImageResource
        //resources are being downloaded using Kingfisher when populating the articlesArray and added to this array

    cell.picView.kf.setImage(with: resource, placeholder: #imageLiteral(resourceName: "LS_logo_male")){ (image, error, cacheType, imageUrl) in
        cell.setNeedsLayout()
    }


    return cell
}

Upvotes: 0

Views: 777

Answers (2)

Umar Muzaffer
Umar Muzaffer

Reputation: 36

Please do UI changes of each cell within 'ArticleCell.swift', so in my case I have used 'Nuke' library which is so far giving me best user experience while downloading and showing images in real time. Please refer below given code for your help:

In main class do like this:

cell.cellImageURL = self.articlesArray?[indexPath.item]

In 'ArticleCell.swift' class do like this:

var cellImageURL: String {
  didSet {
     if let request: NSURLRequest = NSURLRequest(URL: cellImageURL) {
     Nuke.loadImage(with: request, options: options, into: picView, progress: nil) { _ in }
     } else {
       picView.image = UIImage(named: "placeholder")
  }
}

Upvotes: 0

karthik
karthik

Reputation: 621

you can get the visible cells from the table view there you can get the image view for each cell. and you can try using kingfisher to update the image. here you are not refreshing the cell. finding the cell and updating the contents. this way it wont look like flashing.

Upvotes: 0

Related Questions