My UIcollectionview Does Not Scroll Smoothly Using Swift

UICollectionView is not scrolling smooth on reload data

You should only update your UI from the main thread, so do this instead...

Swift 2.x :

           dispatch_async(dispatch_get_main_queue()), {
self.DataTableCollectionView.reloadData()
}

Swift 3 :

DispatchQueue.main.async {
self.DataTableCollectionView.reloadData()
}

If that doesn't solve the problem then you may ALSO need to call loadMoreData() on a background thread...

Swift 2.x :

        dispatch_async(dispatch_get_global_queue(QOS_CLASS_UTILITY, 0)){
self.loadMoreData()
}

Swift 3 :

DispatchQueue.global(qos: DispatchQoS.QoSClass.default).async {
self.loadMoreData()
}

It is unclear exactly how your code works since you have not shown us your loadMoreData() function. It actually seems thats you may be meaning to call DataTableCollectionView.reloadData() from inside loadMoreData , but I cannot tell without seeing the function. Please add the additional code

Collection view not scroll smoothly when fetch data from firebase using Swift?

I found the solution for my bug. Seem the collectionView is reload a lot of time depend on number of sending message. So the solution is quite easy, just remove the collectionView reload data

Not scrolling smooth on UICollectionViewController for large data loading

Do not reload collectionView in cellForItem, delete this part:

    DispatchQueue.main.async {

self.collectionView?.reloadData()

}

And delete most of those reloadData calls that you have there. It's a heavyweight operation - call it only when you really need to reload it - meaning when the data model changes for the whole collection view.

Sidenote:

Don't use indexPath.row, but rather indexPath.item, row is used for UITableViews, not for UICollectionViews.

UICollectionview Scrolling choppy when loading cells

So anybody having scrolling issues should do this

add these 2 lines after your dequeue

cell.layer.shouldRasterize = YES;
cell.layer.rasterizationScale = [UIScreen mainScreen].scale;

CollectionView in TableViewCell scroll isn't smooth in swift3

Download Image Url Data in collectionView: UICollectionView, cellForItemAt indexPath: IndexPath delegate - ASYNCHRONOUSLY

Create a Class

First cache the image Data with respect to key Image URL

Why do we need to cache the data?

Because - While scrolling we don't need to download Image Every Time so Cache the already downloaded Image Data and return the cache data

I created a class name: AsyncImageLoader then I created a function which is a completion handler which returns the data after completion of downloading Image

class AsyncImageLoader {

static let cache = NSCache<NSString, NSData>()

class func image(for url: URL, completionHandler: @escaping(_ image: UIImage?) -> ()) {

DispatchQueue.global(qos: DispatchQoS.QoSClass.background).async {

if let data = self.cache.object(forKey: url.absoluteString as NSString) {
DispatchQueue.main.async { completionHandler(UIImage(data: data as Data)) }
return
}

guard let data = NSData(contentsOf: url) else {
DispatchQueue.main.async { completionHandler(nil) }
return
}

self.cache.setObject(data, forKey: url.absoluteString as NSString)
DispatchQueue.main.async { completionHandler(UIImage(data: data as Data)) }
}
}

}

How to use AsyncImageLoader Class?

See below How I used in collectionView: UICollectionView, cellForItemAt indexPath: IndexPath

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

let yourCell = collectionView.dequeueReusableCell(withReuseIdentifier: "CustomCollectionCell", for: indexPath) as! CustomCollectionCell

yourCell.url = URL(string: "\(item_ImageUrl)")

return yourCell
}

CustomCollectionCell

class CustomCollectionCell: UICollectionViewCell {

@IBOutlet weak var cellImageDisplayView: UIImageView!

var url: URL? {
didSet {
if let url = url {

cellImageDisplayView.image = nil

AsyncImageLoader.image(for: url, completionHandler: { (image) in

DispatchQueue.main.async {

if let img = image {

self.cellImageDisplayView.image = img

}else{
let dummyImage = UIImage(named: "img_u")
self.cellImageDisplayView.image = dummyImage
}
}

})

}else{

let dummyImage = UIImage(named: "img_u")
self.cellImageDisplayView.image = dummyImage

}
}
}

}


Related Topics



Leave a reply



Submit