【问题标题】:View updates too late in Collection View after scrolling滚动后在集合视图中查看更新太晚
【发布时间】:2017-03-08 13:15:02
【问题描述】:

我在每个单元格内都有带有图像的 CollectionView。 当我滚动这个集合时,新单元格(indexPath = 10)包含来自旧单元格(indexPath = 1)的图像,然后调用此方法:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

}

我将图像 URL 设置为新视图。 更新前滚动后隐藏旧图像的最佳方法是什么?

【问题讨论】:

  • 你是从 url 下载图片吗?
  • 在使用 ulr 请求新图像之前,尝试将图像设置为零。这样在从服务器接收到图像之前它将是空的
  • 这是由于单元格的重用。你能把func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell的代码贴出来吗
  • 异步图像的工作方式与 tihs 类似,您只能在加载前使用占位符

标签: ios swift scroll uicollectionview


【解决方案1】:

您需要在重用时清理 CollectionViewCell 中的图像。 覆盖 CollectionViewCell 子类中的prepareForReuse 函数。

  override func prepareForReuse() {
        super.prepareForReuse()
        // Set the cell's imageView's image to nil
        self.imageView.image = nil
    }

【讨论】:

  • 请注意,此方法从 iOS 8 开始可用,以前必须由程序员在将单元格出列后完成
  • @RohanBhale 这不是真的。 CollectionView 从 iOS6 开始可用,prepareForReuse 从 iOS6 开始也可用。请查看 API 文档截图imgur.com/a/DjEjo
  • @dirtydanee UICollectionReusableView 从 iOS 6 开始可用,该方法从 iOS 8 开始可用
  • @RohanBhale 根据文档,这不是真的。
  • @dirtydanee 我是专门为 swift 说的,因为有问题的代码在 swift 中
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多