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

标签 ios swift swift4

我有一个 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/

相关文章:

ios - 游戏中心登录对话框未出现

ios - UIBarButton 不会显示在我的 UINavigationController 中

ios - 在ios中制作条件标签栏

swift - 如何创建ZkSync账户?

swift - 尝试打开 Realm 数据库时出现错误 "unsupported version"

ios - Swift 4 使用 KVO 监听音量变化

ios - 我可以在单击按钮时以编程方式更改 Localizable.strings 文件而不重新启动应用程序吗

ios - 离开时 View 是 "reloaded"吗?

arrays - 对于集合类型,为什么remove方法有返回值而insert没有返回值

Swift 获取协议(protocol)的所有 KeyPath