TableViewCell滚动中的CollectionView在swift3中不流畅 [英] CollectionView in TableViewCell scroll isn't smooth in swift3

查看:246
本文介绍了TableViewCell滚动中的CollectionView在swift3中不流畅的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In my project CollectionView in TableViewCell isn't display and I added

cell.collectionView.reloadData()

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

in my code. After I added, CollectionView displayed in TableViewCell, but ScrollView isn't smooth. How to solve this problem. If someone have any experience or ideas help me. Thanks.

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

CollectionView in my project,

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 委托- ASYNCHRONOUSLY

创建一个类

首先根据关键图像URL缓存图像数据

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

我创建了一个类名: AsyncImageLoader 然后创建了一个函数,它是一个完成处理程序,该函数在完成下载图像后会返回数据

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)) }
    }
}

} 

如何使用AsyncImageLoader类?

How to use AsyncImageLoader Class?

请参见下文 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

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

        }
    }
 }

}

这篇关于TableViewCell滚动中的CollectionView在swift3中不流畅的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆