ios - 为什么我的 Collectionview cell.image 不见了并且乱七八糟?

标签 ios swift uicollectionview

所以在我的 Collection View 单元格中,我有文本和图像:这是我在 CollectionViewLayout 中的代码。

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

    if let content = HomeCollectionViewController.posts[indexPath.item].content {
        let spaceForPostContentLabel = NSString(string: content).boundingRect(with: CGSize(width: view.frame.width - 32, height: 120), options: NSStringDrawingOptions.usesFontLeading.union(NSStringDrawingOptions.usesLineFragmentOrigin), attributes: [NSFontAttributeName: UIFont.systemFont(ofSize: 15)], context: nil)

        if HomeCollectionViewController.posts[indexPath.item].imageURL != nil {
            return CGSize(width: view.frame.width, height: spaceForPostContentLabel.height + postImageViewOriginHeight + 168.0)
        } else {
            return CGSize(width: view.frame.width, height: spaceForPostContentLabel.height + 152.5)
        }
    } else {
        return CGSize(width: view.frame.width, height: 408.5)
    }
}

第一次加载时一切都很好。但是当我向下滚动并再次向上滚动时,一切都变得一团糟,图像消失了,并且图像应该存在的地方出现了巨大的空白。这与 dequeReusableIdentifier 有关系吗?

Note: This error only happen for the first cell, other cell that has image works fine

最佳答案

这可能是由于出队的工作原理而发生的。即使您有 100 个单元格,同时加载的单元格也会受到限制,因此在此限制之后,您将在滚动时开始重复使用旧单元格。

有时我已经遇到了同样的问题,主要是在使用图像时,我发现的最佳方法是对图像使用缓存。

下面我发布了一个使用 AlamofireImage 的示例创建缓存(但您可以使用您喜欢的缓存,甚至是 Swift 库提供的内置缓存。

import AlamofireImage

let imageCache = AutoPurgingImageCache()

class CustomImageView: UIImageView {

    var imageUrlString: String?

    func loadImageFromURL(_ urlString: String){

            imageUrlString = urlString

            let url = URL(string: urlString)

            image = nil

            if let imageFromCache = imageCache.image(withIdentifier: urlString) {
                self.image = imageFromCache
                return
            }

            URLSession.shared.dataTask(with: url!, completionHandler: { (data, response, error) in

                if error != nil {
                    print(error)
                    return
                }

                DispatchQueue.main.async(execute: {

                    let imageToCache = UIImage(data: data!)

                    if self.imageUrlString == urlString {
                        self.image = imageToCache
                    }

                    imageCache.add(imageToCache!, withIdentifier: urlString)
                })

            }).resume()
    }

}

基本上,我正在创建 UIImageView 的子类,并将图像 URL 添加为要保留在缓存中的图像的键。每次我尝试从互联网加载图像时,我都会检查该图像是否已在缓存中,如果是,我将图像设置为缓存中的图像,如果没有,我会从互联网异步加载它.

关于ios - 为什么我的 Collectionview cell.image 不见了并且乱七八糟?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43288057/

相关文章:

iOS 主细节秒

ios - 在 TableView 中找不到 UIWebView

ios - 如何将 UICollectionViewFlowLayout 置于两列中心

ios - UICollectionViewCell Segue 到新的 ViewController

ios - Swift 数组 : Keeping the order while filtering an array using another

ios - 将 View 动画化到 collectionView 中的选定单元格或 scrollView 中的 tableView

ios - 不使用第三方库和 social.framework 将 Twitter 集成到 iOS

ios - 如何在 Swift 协议(protocol)可选函数中使用枚举作为参数

ios - CollectionView 添加额外的单元格 "Add more"

ios - 如何从字符串中解析十进制而不丢失精度?