TableView Cell 中的 PFImageView 加载了错误的图像
PFImageView in a TableView Cell loads wrong image
我想我遇到了一个非常奇怪的问题。在我的一些 TableView 中,当我从 Parse 加载图像时,没有任何数据的单元格有时会显示其他图像。
我使用代码的方式是检查 Parse 上的文件是否存在,如果有图片,PFImageView
会在每个单元格的背景中加载图像。
但是,如果数据库中没有存储图像,PFImageView
应该使用作为占位符的本地图像。然而,在我的 PFTableView
中,没有图像数据的细胞经常使用其他细胞的图像。有谁知道为什么?或者知道修复方法?
代码如下:
if business["businessImage"] as? PFFile != nil {
var file: PFFile = business["businessImage"] as PFFile
cell.businessPhoto.file = file
cell.businessPhoto.loadInBackground()
}
else {
cell.businessPhoto.image = UIImage(named: "placeholder user photo")
}
是不是因为我用的是loadInBackground()
而不是loadInBackgroundWithBlock()
?
当您滚动浏览表格视图时,单元格会被重复使用。之前在该单元格中显示的图像不会被清除。您可以使用 UITableViewCell prepareForReuse method or UITableView delegates didEndDisplayingCell / willDisplayCell 使图像无效并取消加载或使用该单元格。
更新
试试这个:
func tableView(tableView: UITableView, didEndDisplayingCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
cell.businessPhoto.file.cancel()
cell.businessPhoto.image = nil
}
确保在这里使用自定义单元格而不是 UITableViewCell class
题目没有给出根据indexPath设置业务的代码。希望这只是基于行的数组中的简单查找。
已发布代码的一个特定问题是,在您执行异步获取的情况下,它不会立即设置 cell.businessPhoto.image
。
您将看到的效果是,在提取正确的图像时,单元格将包含另一行的图像(由于重用)。解决方法是无条件设置占位图片
第二个问题是可选的,但几乎是必需的:缓存图像。这样,您就不会在用户滚动时不断重新获取。这会导致您的 cellForRowAtIndexPath 代码中出现不同的组织:
// this outer conditional is your original code
if (this business has a "businessImage" PFFile) {
// new: check for cached image
if (the image is cached) {
set cell image to the cached image
} else {
// new: always set a placeholder, maybe a special one for fetching
set cell image to a placeholder (one that indicates fetching)
asynch fetch the image, with completion block {
cache the image
set cell image to the image
}
}
} else {
set cell image to a placeholder (one that indicates no image)
}
请注意,我们在每种情况下都会立即设置单元格图像——即使是在我们开始获取时也是如此。这样做,就不需要实现 prepareForReuse 或 didEndDisplay 挂钩。
不使用缓存,我找到的解决方法是先将 cellForRowAtIndexPath
中的图像文件设置为占位符图像,然后如果在服务器上找到图像对象,则将单元格图像设置为新文件,然后在后台加载它。
代码如下:
myCell.profilePic.image = UIImage(named: "placeholder user image")
if let file: PFFile = object["profilePicture"] as? PFFile {
myCell.profilePic.file = file
myCell.profilePic.loadInBackground()
}
感谢大家的帮助!
我想我遇到了一个非常奇怪的问题。在我的一些 TableView 中,当我从 Parse 加载图像时,没有任何数据的单元格有时会显示其他图像。
我使用代码的方式是检查 Parse 上的文件是否存在,如果有图片,PFImageView
会在每个单元格的背景中加载图像。
但是,如果数据库中没有存储图像,PFImageView
应该使用作为占位符的本地图像。然而,在我的 PFTableView
中,没有图像数据的细胞经常使用其他细胞的图像。有谁知道为什么?或者知道修复方法?
代码如下:
if business["businessImage"] as? PFFile != nil {
var file: PFFile = business["businessImage"] as PFFile
cell.businessPhoto.file = file
cell.businessPhoto.loadInBackground()
}
else {
cell.businessPhoto.image = UIImage(named: "placeholder user photo")
}
是不是因为我用的是loadInBackground()
而不是loadInBackgroundWithBlock()
?
当您滚动浏览表格视图时,单元格会被重复使用。之前在该单元格中显示的图像不会被清除。您可以使用 UITableViewCell prepareForReuse method or UITableView delegates didEndDisplayingCell / willDisplayCell 使图像无效并取消加载或使用该单元格。
更新
试试这个:
func tableView(tableView: UITableView, didEndDisplayingCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
cell.businessPhoto.file.cancel()
cell.businessPhoto.image = nil
}
确保在这里使用自定义单元格而不是 UITableViewCell class
题目没有给出根据indexPath设置业务的代码。希望这只是基于行的数组中的简单查找。
已发布代码的一个特定问题是,在您执行异步获取的情况下,它不会立即设置 cell.businessPhoto.image
。
您将看到的效果是,在提取正确的图像时,单元格将包含另一行的图像(由于重用)。解决方法是无条件设置占位图片
第二个问题是可选的,但几乎是必需的:缓存图像。这样,您就不会在用户滚动时不断重新获取。这会导致您的 cellForRowAtIndexPath 代码中出现不同的组织:
// this outer conditional is your original code
if (this business has a "businessImage" PFFile) {
// new: check for cached image
if (the image is cached) {
set cell image to the cached image
} else {
// new: always set a placeholder, maybe a special one for fetching
set cell image to a placeholder (one that indicates fetching)
asynch fetch the image, with completion block {
cache the image
set cell image to the image
}
}
} else {
set cell image to a placeholder (one that indicates no image)
}
请注意,我们在每种情况下都会立即设置单元格图像——即使是在我们开始获取时也是如此。这样做,就不需要实现 prepareForReuse 或 didEndDisplay 挂钩。
不使用缓存,我找到的解决方法是先将 cellForRowAtIndexPath
中的图像文件设置为占位符图像,然后如果在服务器上找到图像对象,则将单元格图像设置为新文件,然后在后台加载它。
代码如下:
myCell.profilePic.image = UIImage(named: "placeholder user image")
if let file: PFFile = object["profilePicture"] as? PFFile {
myCell.profilePic.file = file
myCell.profilePic.loadInBackground()
}
感谢大家的帮助!