UICollectionView 中的 Nib Cell 注册不显示
Nib Cell registration in UICollectionView does not show
我正在尝试在我的 collectionView
中注册一个 nib
单元格,但它不会显示。
这是我的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
}
我已经正确设置了我的小区标识符
从服务器获取数据后调用collectionView.reloadData()
。
在此部分中,您需要添加
DoshiiServices.shared.getMenu(token: token, locationId: "xxxxxxx") { (bool, drinksMenu) in
self.drinksMenu = drinksMenu
Utilities.run.dismissSVHUD(delay: 0.2)
self.collectionView.reloadData()
}
您应该在设置 drinksMenu
之后调用 collectionView.reloadData()
(在 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
在 drinksMenu
的 didSet
观察者的 collectionView
上调用 reloadData()
,即
var drinksMenu = [Drink]() {
didSet {
self.collectionView.reloadData()
}
}
我正在尝试在我的 collectionView
中注册一个 nib
单元格,但它不会显示。
这是我的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
}
我已经正确设置了我的小区标识符
从服务器获取数据后调用collectionView.reloadData()
。
在此部分中,您需要添加
DoshiiServices.shared.getMenu(token: token, locationId: "xxxxxxx") { (bool, drinksMenu) in
self.drinksMenu = drinksMenu
Utilities.run.dismissSVHUD(delay: 0.2)
self.collectionView.reloadData()
}
您应该在设置 drinksMenu
之后调用 collectionView.reloadData()
(在 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
在 drinksMenu
的 didSet
观察者的 collectionView
上调用 reloadData()
,即
var drinksMenu = [Drink]() {
didSet {
self.collectionView.reloadData()
}
}