UICollectionView中的近邻单元注册不显示

问题描述 投票:0回答:3

我正在尝试在我的nib中注册一个collectionView单元格,但它不会显示。

这是我的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

这是我的DrinkMenuCell

class DrinkMenuCell: UICollectionViewCell {

    @IBOutlet weak var label: UILabel!

    override func awakeFromNib() {
        super.awakeFromNib()

    }

    func configureCell(item: String){

        self.label.text = item


    }//end func

}

我已经正确设置了单元格标识符

enter image description here

swift uicollectionview xib nib
3个回答
1
投票

从服务器获取数据后呼叫collectionView.reloadData()。在此部分,您需要添加

DoshiiServices.shared.getMenu(token: token, locationId: "xxxxxxx") { (bool, drinksMenu) in
   self.drinksMenu = drinksMenu
   Utilities.run.dismissSVHUD(delay: 0.2)
   self.collectionView.reloadData()
}

1
投票

应该在设置collectionView.reloadData()的位置(在drinksMenu块中)之后调用getMenu

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

0
投票

reloadData()collectionView观察者中,即didSet上调用drinksMenu,>]

var drinksMenu = [Drink]() {
    didSet {
        self.collectionView.reloadData()
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.