除非滚动,否则不会加载UICollectionViewCells

时间:2018-09-12 18:51:48

标签: swift uicollectionview uicollectionviewcell

我有一个UICollectionView,并用单元格填充了它。

我有一个正在加载的动画,该动画会停止以指示所有单元格已被填充,但是直到我滚动(可能需要尝试几次),这些单元格才会显示。

这与此类似:

UICollectionView not loading fully until I scroll

但是区别是我的不会部分装载-但根本不会装载。

我的UICollectionViewDataSource

extension ProductsCollectionViewController: UICollectionViewDataSource
{

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
    {
        if searchActive
        {
            return filtered.count
        }
        else
        {
            return products.count    //return number of rows in section
        }
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
    {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "product_collection_cell", for: indexPath) as! ProductsCollectionViewCell
        cell.ProductImageView.image = nil
        cell.ProductName.text = nil
        cell.ProductPrice.text = nil
        cell.productUniqueID = nil

        let prodInCell =  searchActive ? filtered[indexPath.row] : products[indexPath.row]

        let prodID = prodInCell.getUniqueID()
        let dbRef = Storage.storage().reference().child(prodID).child("pic0.jpg")
        cell.contentMode = .scaleAspectFit
        cell.ProductImageView.image = #imageLiteral(resourceName: "DefaultProductImage")
        dbRef.downloadURL(completion:
            {
                url, error in
                if let error = error
                {
                    print (error)
                }
                else if let url = url
                {
                    cell.ProductImageView.loadImageUsingUrlString(urlString: url.absoluteString)
                    cell.ProductImageView.contentMode = UIViewContentMode.scaleToFill
                    cell.layoutIfNeeded()
                }
        })
        cell.ProductImageView.clipsToBounds = true

        //cell.ProductName = UILabel()
        cell.ProductName.text = prodInCell.getName()

        //cell.ProductPrice = UILabel()
        cell.ProductPrice.text = String(prodInCell.getPrice())
        cell.productUniqueID = prodInCell.getUniqueID()
        return cell
    }

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)
    {
        // Display selected Item
        prodToLoad = products[indexPath.row]
        performSegue(withIdentifier: "view_product_information", sender:self  )
    }

    // Swift 3.0
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: CGFloat((collectionView.frame.size.width / 3) - 20), height: CGFloat(100))
    }
}

我在做什么错了?

1 个答案:

答案 0 :(得分:1)

cellForRowAt内部,您有2个异步操作

1-

dbRef.downloadURL(completion:

2-

cell.ProductImageView.loadImageUsingUrlString(urlString: url.absoluteString)

考虑使用SDWebImage,因为这将多次下载同一张图片

//

在数组项自定义类/结构内创建一个可选的urlStr属性,并像下载时一样对其进行设置

if let str = prodInCell.urlStr {

    cell.ProductImageView.sd_setImage(with: URL(string:str), placeholderImage: UIImage(named: "placeholder.png"))
}
else {

       let dbRef = Storage.storage().reference().child(prodID).child("pic0.jpg")
       cell.contentMode = .scaleAspectFit
       cell.ProductImageView.image = #imageLiteral(resourceName: "DefaultProductImage")
       dbRef.downloadURL(completion:
        {
            url, error in
            if let error = error
            {
                print (error)
            }
            else if let url = url
            {
               prodInCell.urlStr = url.absoluteString // store for upcoming need
               cell.ProductImageView.sd_setImage(with: URL(string:url.absoluteString), placeholderImage: UIImage(named: "placeholder.png"))      
               cell.ProductImageView.contentMode = UIViewContentMode.scaleToFill
               cell.layoutIfNeeded()
            }
    })

 }

//

还记得

import SDWebImage

并设置项目包中要加载的所有占位符图像,直到下载真实图像为止