簡體   English   中英

Swift willDisplayCell和PinterestLayout不起作用

[英]Swift willDisplayCell & PinterestLayout doesnt work

我按照本教程使用pinterest之類的collectionView。

https://www.raywenderlich.com/164608/uicollectionview-custom-layout-tutorial-pinterest-2

[更新]

'willDisplay cell'方法對我不起作用。
當displaycell方法喚醒但不重新加載collectionView時,我得到了新數據。
我把所有的代碼。 請幫我!

import UIKit
import Kingfisher

class CategoryPageVC: UIViewController , UICollectionViewDelegate , UICollectionViewDataSource {

    var mainID:Int!
    var categoryData = [CategoryPageData]()

    var descriptionText: String!
    @IBOutlet var descriptionLabel: UILabel!

    var phoneLanguageUser:String!

    @IBOutlet var loadingView: UIView!
    @IBOutlet var collectionView: UICollectionView!
    @IBOutlet var emptyView: UIView!
    @IBOutlet var gifImageView: UIImageView!

    override func viewDidLoad() {
        super.viewDidLoad()

        print(self.mainID!)


        loaderImages = createImageArray(total: 89, imagePrefix: "Loader")
        animate(imageView: gifImageView, images: loaderImages)

        phoneLanguageUser = NSLocale.current.languageCode
        if phoneLanguageUser != "tr" && phoneLanguageUser != "en" {
            phoneLanguageUser = "en"
        }

        self.loadingView.isHidden = false
        self.loadingView.backgroundColor = UIColor.white
        self.emptyView.isHidden = true

        self.collectionView.delegate = self
        self.collectionView.dataSource = self

        if let layout = self.collectionView?.collectionViewLayout as? PinterestLayout {
            layout.delegate = self
        }

        // --------------------------------- JSON DATA ----------------------------------------


        let url = URL(string: "....")
        let task = URLSession.shared.dataTask(with:url!, completionHandler: {(data, response, error) in
            ....
            ....
            ....
        })
        task.resume()

        // --------------------------------- JSON DATA END ----------------------------------------



    }

    override func viewDidAppear(_ animated: Bool) {

        let when = DispatchTime.now() + 5 // change 2 to desired number of seconds
        DispatchQueue.main.asyncAfter(deadline: when) {

            if self.categoryData.count == 0 {
                self.loadingView.isHidden = true
                self.emptyView.isHidden = false
            }

        }

        self.collectionView.reloadData()

        print(categoryData.count)
    }

    // --------------------------- COLLECTION VIEW DATA ----------------------------------

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return categoryData.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "PPColCell", for: indexPath as IndexPath) as! PPCollectionCell
        cell.imageView.image = self.categoryData[indexPath.row].image
        return cell
    }

    // --------------------------- COLLECTION VIEW DATA END --------------------------------


    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let itemSize = (collectionView.frame.width - (collectionView.contentInset.left + collectionView.contentInset.right + 10)) / 2
        return CGSize(width: itemSize, height: itemSize)
    }

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        performSegue(withIdentifier: "categoryToProductSegue", sender: self.categoryData[indexPath.row].uID)
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "categoryToProductSegue" {
            if let destination = segue.destination as? ProductPageVC {
                if let UID = sender as? String {
                    destination.UID = UID
                }
            }
        }
    }

    var loaderImages: [UIImage] = []
    func createImageArray(total: Int, imagePrefix:String) -> [UIImage] {
        var imageArray : [UIImage] = []
        for imageCount in 1..<total {
            let imageName = "\(imagePrefix)-\(imageCount).png"
            let image = UIImage(named: imageName)!
            imageArray.append(image)
        }
        return imageArray
    }
    func animate(imageView: UIImageView, images: [UIImage]) {
        imageView.animationImages = images
        imageView.animationDuration = 2
        imageView.animationRepeatCount = 0
        imageView.startAnimating()
    }


    @objc func loadMoreData(){

        let url = URL(string: ...)
        ....
        ...
        .... 
        })
        task.resume()

    }


    func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
        let lastElement = categoryData.count - 2
        if indexPath.row == lastElement {
            loadMoreData()
        }
    }

}

extension CategoryPageVC: UICollectionViewDelegateFlowLayout {

}

extension CategoryPageVC: PinterestLayoutDelegate {
    func collectionView(_ collectionView: UICollectionView,
                        heightForPhotoAtIndexPath indexPath:IndexPath) -> CGFloat {

let oran = self.categoryData[indexPath.item].image.size.width / self.collectionView.frame.size.width * 2
        return self.categoryData[indexPath.item].image.size.height / oran
    }
}

我找到了解決方案。 問題出在PinterestLayout教程上。 我更改了代碼,例如在下面寫下

在PinterestLayout.swift

guard cache.isEmpty == true, let collectionView = collectionView else {
            return
        }

guard let collectionView = collectionView else {
            return
        }

工作正常。

暫無
暫無

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

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