gpt4 book ai didi

ios - 如何防止在 cellForItemAt 中重新加载 UICollectionViewCells?

转载 作者:行者123 更新时间:2023-11-30 11:56:46 25 4
gpt4 key购买 nike

我有一个 CollectionViewController,在其中我联系 API 以根据一组坐标下载图像。我在 cellForItemAt 函数中调用此代码,此时它会使用 Flickr 中的图像实时更新单元格的图像。这工作得很好。

但是,当向上或向下滚动时,它会调用此代码并再次更新单元格,而我希望它查看现有单元格,确定它们是否已被填充,并且根本不运行此代码。

我尝试在网络代码之前实现逻辑,检查 imageView.images 是否已存在于我分配给它们的本地结构中,但这似乎无法正常工作。

是否有一个简单的方法来告诉 cellForItemAt“对于已经有图像的单元格,不要寻找更多”?

这是我当前的代码:

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

let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "imageCell", for: indexPath as IndexPath) as! CollectionViewCell

// Get images = using the URL
FlickrClient.sharedInstance().getImagesFromFlickr(latitude: selectedPin.lat, longitude: selectedPin.lon, page: pageCount) { (pin, error) in

if let pin = pin {
let url = pin.images[indexPath.item].imageURL
let data = try? Data(contentsOf: url)
performUIUpdatesOnMain {
cell.imageView.image = UIImage(data: data!)
cell.imageView.contentMode = .scaleAspectFill
}
}
}
return cell
}

最佳答案

使用 SDwebImage libray 从 url 加载图像。

https://github.com/rs/SDWebImage

在行的单元格上调用类似这样的内容:

let url = pin.images[indexPath.item].imageURL
cell.imageView.sd_setImage(with: url, placeholderImage: UIImage(named: "placeholder.png"))

关于ios - 如何防止在 cellForItemAt 中重新加载 UICollectionViewCells?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47722795/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com