Nate
Nate

Reputation: 536

Table view images not loading with or without DispatchQueue

I'm using the SDWebImage library to load images into my table cells as imageView. I'm having a similar issue to this post; my tableView loads but without the images. When I click on a cell and then go back to the main page, the images have finally loaded. But I want the images to load without having to do that (without any interaction).

However, removing the DispacheQueue.main.async doesn't fix the issue; the images still aren't loaded. I read that cell.setNeedsLayout() and cell.layoutIfNeeded() were supposed to help fix this, but they didn't.

myURLss is my array of URLs, by the way

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "PostCell")

        DispatchQueue.main.async {
            cell?.imageView?.sd_setImage(with: self.myURLss[indexPath.row])
            cell?.setNeedsLayout()
        }

    return cell!

}

Upvotes: 0

Views: 1759

Answers (2)

Fırat URASLI
Fırat URASLI

Reputation: 11

you can reload row after loaded image.

    cell?.imageView?.sd_setImage(with: URL(string: your.url), completed: {_,_,_,_ in 
        self.tableView.reloadRows(at: [indexPath], with: .none)
    })

Upvotes: 0

iOS Geek
iOS Geek

Reputation: 4855

I tried using SDWebImage Check Project at Link -

https://drive.google.com/open?id=1BQ_3fFvD04BnGa5uRqbAtU4DWGn_Pzpq

I created My custom cell class in which imageView is Connected and updating it in cellFOrRowAt of TableView

class CustomCell: UITableViewCell {

    //My ImageView
    @IBOutlet weak var myImageView: UIImageView!

    override func awakeFromNib()
    {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}

My TableView CellForRowAt

 //Setting cells data
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {

        let cell = self.myTableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier) as! CustomCell

        DispatchQueue.main.async
            {
            cell.myImageView.sd_setImage(with: self.myURLss[indexPath.row], completed: nil)
        }

        return cell
    }

Also as you are using SDWebImage Library they provide set of options To us

if still image in your code does not show try using below line

cell.myImageView.sd_setImage(with: self.myURLss[indexPath.row], placeholderImage: #imageLiteral(resourceName: "malee"), options: .continueInBackground, completed: nil)

Hope It helps

Upvotes: 2

Related Questions