I have a UICollectionView
, it runs as it should when I add fixed data to it, but if I reloaded it again (getting data from service) the scroll becomes slow or it might stop scrolling.
This is the code:
@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
}
Any solution for my case?