Table 使用或不使用 DispatchQueue 查看未加载的图像

Table view images not loading with or without DispatchQueue

我正在使用 SDWebImage 库将图像作为 imageView 加载到我的 table 单元格中。 我有一个类似的问题 这个;我的 tableView 加载但没有图像。当我点击一个单元格然后返回主页时,图像终于加载了。但我希望无需执行此操作(无需任何交互)即可加载图像。

但是,删除 DispacheQueue.main.async 并不能解决问题;图像仍未加载。我读到 cell.setNeedsLayout() 和 cell.layoutIfNeeded() 应该可以帮助解决这个问题,但他们没有。

顺便说一下,myURLss 是我的 URL 数组

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!

}

我尝试在 Link -

使用 SDWebImage Check Project

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

我创建了我的自定义单元格 class,其中连接了 imageView,并在 TableView 的 cellFOrRowAt 中更新了它

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
    }

}

我的 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
    }

此外,当您使用 SDWebImage 库时,他们为我们提供了一组选项

如果您的代码中的静止图像未显示,请尝试使用下面的行

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

希望对你有帮助

您可以在加载图像后重新加载行。

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