簡體   English   中英

TableViewCell滾動中的CollectionView在swift3中不流暢

[英]CollectionView in TableViewCell scroll isn't smooth in swift3

這是我在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

    }

}

下載圖像URL數據中collectionView: UICollectionView, cellForItemAt indexPath: IndexPath委托- ASYNCHRONOUSLY

建立課程

首先根據關鍵圖像URL緩存圖像數據

為什么我們需要緩存數據?

因為-在滾動時我們不需要每次都下載圖像,因此緩存已下載的圖像數據並返回緩存數據

我創建了一個類名稱: AsyncImageLoader然后創建了一個函數,該函數是一個完成處理程序,該函數在完成下載圖像后返回數據

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類?

參見下面我如何在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

        }
    }
 }

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

添加以上代碼后,我的項目變得更加順利。 我將盡力而為,並提供最佳解決方案。 謝謝

這是因為您直接從url中獲取數據,然后從主線程中的數據獲取圖像。 您應該使用圖像下載庫從url下載圖像。這是從url異步下載圖像的很好的第三方。

https://github.com/rs/SDWebImage

試試這個,我100%肯定會解決您的問題。

謝謝

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM