【问题标题】:PFImageView in a TableView Cell loads wrong imageTableView Cell 中的 PFImageView 加载错误的图像
【发布时间】:2015-03-06 21:46:28
【问题描述】:

我认为我在这里遇到了一个非常奇怪的问题。在我的某些 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()

【问题讨论】:

    标签: ios uitableview swift parse-platform pfimageview


    【解决方案1】:

    不使用缓存,我找到的解决方法是先将cellForRowAtIndexPath中的图像文件设置为占位符图像,然后如果在服务器上找到图像对象,则将单元格图像设置为新文件,然后然后在后台加载它。

    代码如下:

            myCell.profilePic.image = UIImage(named: "placeholder user image")
    
            if let file: PFFile = object["profilePicture"] as? PFFile {
                myCell.profilePic.file = file
                myCell.profilePic.loadInBackground()
            }
    

    感谢大家的帮助!

    【讨论】:

      【解决方案2】:

      当您滚动浏览表格视图时,单元格会被重用。之前在该单元格中显示的图像不会被清除。您可以使用 UITableViewCell prepareForReuse 方法或 UITableView 委托 didEndDisplayingCell / willDisplayCell 来取消图像并取消加载或该单元格。

      更新

      试试这个:

      func tableView(tableView: UITableView, didEndDisplayingCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
        cell.businessPhoto.file.cancel()
        cell.businessPhoto.image = nil
      }
      

      请确保您使用自定义单元格类而不是 UITableViewCell

      【讨论】:

      • 我将在哪里实现这些?就在我返回cellForRowAtIndexPath 中的单元格之前?编辑:试过了,它只是让所有的图像空白。
      • 响应更新:当我尝试使用我的PFTableViewCell 类实现此方法时,编译器说由于类型不兼容,我无法覆盖该方法。
      • 是的,这是可能的解决方案之一。主要是在再次显示之前重置单元格。 prepareForReuse / didEndDisplayingCell 通常用于此目的,但在 cellForRowAtIndexPath 中没有什么可以阻止您这样做
      • 就像我提到的,为什么我尝试实现这个方法我得到错误,我试图用不兼容的类型覆盖一个方法。
      • 试图在没有optional 的情况下实现didEndDisplayingCell 会做出任何改变吗?在cellForRowAtIndexPath 中,您是否在开始加载新图像之前或之后(应该之前)取消 + 取消图像加载?
      【解决方案3】:

      问题没有显示基于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 钩子了。

      【讨论】:

      • 这当然是正确的答案,我只是一个新手程序员,还没有开始实现缓存。
      猜你喜欢
      • 1970-01-01
      • 2020-06-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多