【问题标题】:Returns nil if I scroll tableView fast如果我快速滚动 tableView,则返回 nil
【发布时间】:2017-07-11 00:01:21
【问题描述】:

尝试在(Xcode 9 和 Swift 4)中异步加载 tableView 中的图像,似乎我有一个正确的方法,但如果我快速滚动我的 tableView,我的代码将停止工作。所以基本上我发现了零错误
这是我的代码:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CustomTableViewCell


    let feed = feeds[indexPath.row]

    cell.titleLabel.text = feed.title
    cell.pubDateLabel.text = feed.date
    cell.thumbnailImageView.image = nil

    if let image = cache.object(forKey: indexPath.row as AnyObject) as? UIImage {
        cell.thumbnailImageView?.image = image
    } else {

        let imageStringURL = feed.imageUrl

        guard let url = URL(string: imageStringURL) else { fatalError("there is no correct url") }

        URLSession.shared.downloadTask(with: url, completionHandler: { (url, response, error) in

            if let data = try? Data(contentsOf: url!) {
                DispatchQueue.main.async(execute: {
                    guard let image = UIImage(data: data) else { fatalError("can't create image") }

                    let updateCell = tableView.cellForRow(at: indexPath) as! CustomTableViewCell // fast scroll issue line

                    updateCell.thumbnailImageView.image = image
                    self.cache.setObject(image, forKey: indexPath.row as AnyObject)
                })
            }
        }).resume()
    }

    return cell
}

我有问题在线:

let updateCell = tableView.cellForRow(at: indexPath) as! CustomTableViewCell

如果我慢慢向下滚动,一切正常,不会出现任何错误。

有谁知道我哪里出错了?

【问题讨论】:

  • 为什么不删除该行,将 updateCell 更改为单元格,返回一个占位符单元格,用户可以在其中看到它正在加载,并添加一个完成处理程序,该处理程序将在下载后更改单元格的图像?
  • @J.Doe 该行检查单元格是否可见,正如我所理解的那样,因为如果我不使用该行,我会看到图像在找到要显示的合适图像时一直在变化。跨度>
  • 我认为您应该在 CustomTablieViewCell 上使用 prepareForReuse 函数。并将图像设置回零。
  • 我不知道是否有任何特定原因不使用其中一个库(Alamofire-Image)[github.com/Alamofire/AlamofireImage](Kingfisher)[github.com/onevcat/Kingfisher] 只是设置了 url 和占位符跨度>
  • @mihatel 只是想了解基础知识

标签: ios swift uitableview asynchronous cell


【解决方案1】:

如果您尝试使用 tableView.cellForRow(at:) 获取的单元格当前不可见,则可能会发生这种情况。
为避免崩溃,您可以使用以下选项:

let updateCell = tableView.cellForRow(at: indexPath) as? CustomTableViewCell // fast scroll issue line
updateCell?.thumbnailImageView.image = image

保持一切原样,我希望它应该可以正常工作。

【讨论】:

  • 谢谢。没想到这里可能有什么问题)
【解决方案2】:

您可以考虑一个流行的 UIImage 扩展库,就像我在评论中所说的那样,例如 AlamofireImage 并使用占位符设置缩略图,一旦图像准备好,它将被自动替换。

我更改的另一件事不需要 updateCell 我将其删除。

请添加占位符图像并测试它应该可以工作,抱歉我没有完全检查语法。

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CustomTableViewCell


let feed = feeds[indexPath.row]

if let image = cache.object(forKey: indexPath.row as AnyObject) as? UIImage {
    cell.thumbnailImageView?.image = image
} else {

    let imageStringURL = feed.imageUrl

    guard let url = URL(string: imageStringURL) else { fatalError("there is no correct url") }


    cell.thumbnailImageView.af_setImage(withURL : url, placeholderImage: <your_placeholderImage>)


return cell

}

【讨论】:

  • 我可以使用不同的库来做到这一点,但我想找到自己的错误,看看我理解的错误。
猜你喜欢
  • 1970-01-01
  • 2012-11-21
  • 1970-01-01
  • 1970-01-01
  • 2015-11-11
  • 2021-11-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多