iOS - 收藏视图图像在滚动时消失,有时会重叠



从服务器下载图像并填充集合视图(网格 3 * n(时。我遇到了故障.

我尝试了几乎所有可能的方法,例如在重用单元格之前使图像视图为零,并使用 GCD 从主线程更新图像视图。

但我仍然面临故障。

故障是当我滚动集合视图时,下载的图像重叠,有时会从集合视图单元格中消失。

我不知道是什么导致图像消失(曾经下载过(。

任何建议。

    private func cellForDropbox(cell:GridCell,indexPath:IndexPath) -> GridCell {
    let filename = self.filenames[(indexPath as NSIndexPath).row]
    cell.imageView.backgroundColor = UIColor.lightGray
    cell.imageView.image = nil
    DropboxClientsManager.authorizedClient?.files.getTemporaryLink(path: filename).response(completionHandler: { (response, error) in
            if let url = response {
                cell.imageView.sd_setImage(with: URL(string:url.link), placeholderImage: nil, options: .refreshCached, completed: { (img, error, cacheType, url) in
                })
            } else {
                print("Error downloading file from Dropbox: (error!)")
            }
        })
    return cell
}

如果不想完成图像下载的回调,请尝试此操作。

private func cellForDropbox(cell:GridCell,indexPath:IndexPath) -> GridCell {
    let filename = self.filenames[(indexPath as NSIndexPath).row]
    cell.imageView.backgroundColor = UIColor.lightGray
    DropboxClientsManager.authorizedClient?.files.getTemporaryLink(path: filename).response(completionHandler: { (response, error) in
            if let url = response {
            cell.imageView.sd_setImage(with: URL(string: url.link), placeholderImage: UIImage(named: "placeholder.png"))

            } else {
                print("Error downloading file from Dropbox: (error!)")
            }
        })
    return cell
}

最新更新