我的单元格中有一张图片和一些标签。如果我的单元格多于页面上可以容纳的单元格,向下滚动然后向上滚动会立即加载不同的图像,然后加载原始照片。我已经阅读了 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/