ios - 向下滚动和向上滚动后 UITableViewCell 中的图像发生变化

标签 ios swift uitableview firebase firebase-realtime-database

我的单元格中有一张图片和一些标签。如果我的单元格多于页面上可以容纳的单元格,向下滚动然后向上滚动会立即加载不同的图像,然后加载原始照片。我已经阅读了 StackOverflow 以了解什么对我的情况有用,但到目前为止我找不到任何东西,因为我的 UITableView 在 ViewController 中。

以下是我如何将我的内容加载到我的单元格中:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! PostTableViewCell
    // Configure the cell...
    let post = self.posts[indexPath.row] as! [String: AnyObject]
    cell.titleLabel.text = post["title"] as? String
    cell.priceLabel.text = post["price"] as? String
    if let imageName = post["image"] as? String {
        let imageRef = FIRStorage.storage().reference().child("images/\(imageName)")
        imageRef.data(withMaxSize: 25 * 1024 * 1024, completion: { (data, error) -> Void in if error == nil {
            let image = UIImage(data: data!)
            UIView.animate(withDuration: 0.4, animations: {
                cell.titleLabel.alpha = 1
                cell.postImageView.alpha = 1
                cell.priceLabel.alpha = 1
            cell.postImageView.image = image
            })
        } else {
            print("Error occured during image download: \(error?.localizedDescription)")
            }
        })
    }
    return cell
}

有什么方法可以将 tableView.dequeueReusableCell 更改为不同的东西,这样就不会发生这种情况吗?

最佳答案

在您的 TableView 单元格 PostTableViewCell 中,您需要实现该方法

 override func prepareForReuse() {
    super.prepareForReuse()
    self.postImageView.image = nil
    // Set cell to initial state here, reset or set values
}

单元格保留了它们的旧内容

关于ios - 向下滚动和向上滚动后 UITableViewCell 中的图像发生变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42428131/

相关文章:

php - 错误域=NSCocoaErrorDomain 代码=3840 JSONObject

ios - 选择游戏中心部分目的地

ios - 从一个 View Controller 切换到另一个 View Controller 时出错

ios - 如何使用 NSPredicate 在 CoreData 中查找标题

iphone - 有没有iPhone sdk的TFTP客户端,可以传文件到TFTP服务器?

iphone - 在应用程序版本等之间访问 CoreData 实体

ios - SkSpriteNode在通用游戏中的位置

swift - 如何在UITableView中添加列

ios - 如何在不创建 *Cell.swift 文件的情况下访问 UITableViewCell 对象

ios - 使用 UserDefaults 在 SKScene 之间传递字符串