Search code examples
swiftuicollectionviewxibnib

Nib Cell registration in UICollectionView does not show


I'm trying to register a nib cell in my collectionView but it wont show.

This is my MenuCardVC

import UIKit

class MenuCardVC: UIViewController{


    @IBOutlet weak var collectionView: UICollectionView!

    var drinksMenu = [Drink]()

    var venueId: String?

    //Selected venue
    var venue: Venue? {
        didSet{
            loadDrinksMenu()
        }
    }


    override func viewDidLoad() {
        super.viewDidLoad()


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

        let nib = UINib(nibName: "DrinkMenuCell", bundle: nil)
        self.collectionView.register(nib, forCellWithReuseIdentifier: "DrinkMenuCell")



    }//end viewDidLoad


    func loadDrinksMenu(){

        Utilities.run.showSVHUDWithStatus(uiView: self.view, status: "Loading menu")
        getJWTfromGoogleCloudFunction()


    }//end func

    func getJWTfromGoogleCloudFunction(){

        DoshiiServices.shared.getJWTfromGoogleCloudFunction { (token) in

            self.drinksMenu.removeAll()

            DoshiiServices.shared.getMenu(token: token, locationId: "xxxxxxx") { (bool, drinksMenu) in


                self.drinksMenu = drinksMenu
                Utilities.run.dismissSVHUD(delay: 0.2)

            }//end getMenu

        }//getJWTfromGoogleCloudFunction

    }//end func

}//end class

extension MenuCardVC: UICollectionViewDelegate, UICollectionViewDataSource {

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

        return drinksMenu.count

    }//end func

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "DrinkMenuCell", for: indexPath) as! DrinkMenuCell

        cell.configureCell(item: "BEER") //<---testing

        return cell

    }//end func

}//end extension

This is my DrinkMenuCell

class DrinkMenuCell: UICollectionViewCell {

    @IBOutlet weak var label: UILabel!

    override func awakeFromNib() {
        super.awakeFromNib()

    }

    func configureCell(item: String){

        self.label.text = item


    }//end func

}

I have set my cell identifier correctly

enter image description here


Solution

  • You should call collectionView.reloadData() after where you set drinksMenu (in getMenu block)

    func getJWTfromGoogleCloudFunction(){
    
        DoshiiServices.shared.getJWTfromGoogleCloudFunction { (token) in
    
            self.drinksMenu.removeAll()
    
            DoshiiServices.shared.getMenu(token: token, locationId: "xxxxxxx") { (bool, drinksMenu) in
    
    
                self.drinksMenu = drinksMenu
                Utilities.run.dismissSVHUD(delay: 0.2)
                // Add here!
                self.collectionView.reloadData()
    
            }//end getMenu
    
        }//getJWTfromGoogleCloudFunction
    
    }//end func