的CollectionView在TableViewCell滚动不swift3

问题描述:

Here is my CollectionView in TableViewCell的CollectionView在TableViewCell滚动不swift3

顺利在我的项目CollectionViewTableViewCell不显示,我在我的代码添加

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 

    } 

} 
+0

如何使用只给代表和collectioview的数据源中的tableview的cellforrow方法不重装colletioview –

+0

collectionView.delegate =自 collectionView.dataSource =自 我已经加入。但没有解决问题@JeckyModi –

+0

你在collectionview中加载了什么?如果图像,请确保图像不是高分辨率。 –

下载图像URL数据在collectionView: UICollectionView, cellForItemAt indexPath: IndexPath委托 - ASYNCHRONOUSLY

创建共享实例一类

首先缓存中的图像数据相对于关键图像URL

我们为什么需要缓存数据?

因为 - 虽然滚动我们并不需要下载图像的每一次这样高速缓存中已经下载的图像数据并返回缓存数据

我创建了一个类名:AsyncImageLoader然后我创建了一个函数,它是一个完成处理器,其下载图片

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 
     } 
    } 
} 
+0

哇!在我添加你的代码之后,我的项目现在很流畅。感谢您的最佳答案! –

cell.layer.shouldRasterize = true 
cell.layer.rasterizationScale = UIScreen.main.scale 

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

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

https://github.com/rs/SDWebImage

这种尝试,我100%肯定它会解决你的问题。

感谢