在Swift3中,TableViewCell中的CollectionView并不平滑

这是我在TableViewCell中的CollectionView

在我的项目TableViewCell CollectionView不显示,我添加

 cell.collectionView.reloadData() 

在我的代码。 我添加后, CollectionView显示在TableViewCell ,但ScrollView不平滑。 如何解决这个问题呢。 如果有人有任何经验或想法帮助我。 谢谢。

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "homecell") as! HomeCategoryRowCell let mainThemeList = mainHomeThemeTable[(indexPath as NSIndexPath).row] DispatchQueue.main.async { cell.categoryTitle.text = mainThemeList.main_name cell.collectionView.reloadData() } return cell } 

CollectionView在我的项目中,

 extension HomeCategoryRowCell : UICollectionViewDataSource { func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { debugPrint("assetsTable count \(assetsTable.count)") return assetsTable.count } func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "videoCell", for: indexPath) as! HomeVideoCell let list = assetsTable[(indexPath as NSIndexPath).row] let url2 = NSURL(string:list.poster_image_url) let data2 = NSData(contentsOf:url2! as URL) // debugPrint(list.poster_image_url) DispatchQueue.main.async(){ cell.movieTitle.text = list.name cell.imageView.image = UIImage(data: data2! as Data) } return cell } } 

collectionView: UICollectionView, cellForItemAt indexPath: IndexPath下载图像Url数据collectionView: UICollectionView, cellForItemAt indexPath: IndexPath委托 – collectionView: UICollectionView, cellForItemAt indexPath: IndexPath

用共享实例创build一个类

首先caching关键图片URL的图片数据

为什么我们需要caching数据?

因为 – 滚动时,我们不需要每次下载图像,所以caching已经下载的图像数据并返回caching数据

我创build了一个类名: AsyncImageLoader然后我创build了一个函数,它是一个完成处理程序,它在完成下载Image之后返回数据

 class AsyncImageLoader { //For caching the Image Data use NSCache var cache = NSCache<AnyObject, AnyObject>() //Shared Instance class var sharedLoader : AsyncImageLoader { struct Static { static let instance : AsyncImageLoader = AsyncImageLoader() } return Static.instance } //Completion handler function which returns the Image after complete downloading Image func imageForUrl(urlString: String, completionHandler: @escaping (_ image: UIImage?, _ url: String) -> ()) { let data = self.cache.object(forKey: urlString as AnyObject) as? NSData if let goodData = data { //check already we cached the image before or not let image = UIImage(data: goodData as Data) DispatchQueue.main.async(execute: {() in completionHandler(image, urlString) }) return } //if url Str is nil - check it or it crashes if urlString.characters.count <= 0 { DispatchQueue.main.async(execute: {() in completionHandler(nil, urlString) // returns nil }) return } let strToUrl = URL(string: urlString.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!)! URLSession.shared.dataTask(with: strToUrl, completionHandler: { (data, response, error) -> Void in if error == nil{ let image = UIImage(data: data!) self.cache.setObject(data as AnyObject, forKey: urlString as AnyObject) //setting cache of image with key as Image url DispatchQueue.main.async(execute: {() in completionHandler(image, urlString) //completion handler call }) } else { let image = UIImage(named: "logoBlue") //return dummy image if fails DispatchQueue.main.async(execute: {() in completionHandler(image, urlString) }) return } }).resume() } } 

如何使用AsyncImageLoader类?

请参阅下面如何在collectionView: UICollectionView, cellForItemAt indexPath: IndexPath

  func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { AsyncImageLoader.sharedLoader.imageForUrl(urlString: list.poster_image_url) { (image, url) -> () in DispatchQueue.main.async { cell.imageView.image = image } } } 
 cell.layer.shouldRasterize = true cell.layer.rasterizationScale = UIScreen.main.scale 

添加上面的代码后,我的项目变得更加stream畅。 我会尽力而为,并以最好的解决scheme回来。 谢谢

这是因为您直接从url获取数据,然后从主线程中的数据获取图像。 你应该使用图像下载库从url下载图像这是asynchronous下载图像的好第三方。

https://github.com/rs/SDWebImage

试试这个我100%确定它会解决你的问题。

谢谢