gpt4 book ai didi

ios - 重新加载数据时 UICollectionView 滚动性能降低

转载 作者:行者123 更新时间:2023-11-28 12:07:44 25 4
gpt4 key购买 nike

我有一个 UICollectionView,当我向它添加固定数据时它会正常运行,但如果我再次重新加载它(从服务获取数据),滚动会变慢或可能停止滚动。

这是代码:

@IBOutlet var myCollectionView: UICollectionView!

func getAllCategories() {
API.categories { (error: Error?, success: Bool, categories: [Category]) in
if success {
self.categories = categories
print("success")
self.myCollectionView.reloadData()
} else {
print("failed")
}
}
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if(self.categories.count > 0) {
return (self.categories.count - 1)
} else {
return 0
}
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "HomeCollectionViewCell", for: indexPath) as! HomeCollectionViewCell

cell.containerCollectionView.layer.cornerRadius = 20
cell.containerCollectionView.layer.borderWidth = 2
cell.containerCollectionView.layer.borderColor = UIColor(red:219/255.0, green:219/255.0, blue:219/255.0, alpha: 0.5).cgColor;
cell.containerCollectionView.layer.masksToBounds = true
if(self.categories.count > 0) {
cell.category_name.text = self.categories[indexPath.row + 1].name
cell.adds_number.text = String(self.categories[indexPath.row + 1].ads_count) + " إعلان"
cell.fav_number.text = String(self.categories[indexPath.row + 1].fav_count) + " مفضلة"
////// to load URLs images //////
let imageUrl = "http://xxxxxxxx.com/images/categories/" + self.categories[indexPath.row + 1].image
let url = URL(string: imageUrl)
let image = NSData(contentsOf: url!)
if(image != nil){
cell.categoryImageView.image = UIImage(data: image! as Data)
}
}
return cell
}

我的情况有什么解决方案吗?

最佳答案

问题在下面的代码:

////// to load URLs images //////
let imageUrl = "http://souqsahm.com/images/categories/" + self.categories[indexPath.row + 1].image
let url = URL(string: imageUrl)
let image = NSData(contentsOf: url!)
if(image != nil){
cell.categoryImageView.image = UIImage(data: image! as Data)
}

尝试在后台线程中下载图像,或者您可以使用此 api https://github.com/rs/SDWebImage

关于ios - 重新加载数据时 UICollectionView 滚动性能降低,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49093721/

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