UITableview 单元格未显示

UITableview cells aren't showing

UITableview 可见,而单元格不可见。

这是一个订餐应用程序,我正在尝试显示菜单。我已经尝试了所有方法,没有显示错误,但单元格不可见

import UIKit
import FirebaseDatabase
import FirebaseCore


class MenuVC: UIViewController, UITableViewDelegate, UITableViewDataSource {

    var menu = [Food]()
    var ref: DatabaseReference?

    @IBOutlet weak var tableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        tableView.delegate = self
        tableView.dataSource = self
        ref = Database.database().reference()
        loadMenu()
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return menu.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if let cell = tableView.dequeueReusableCell(withIdentifier: "menuCell") as? MenuCell {
            let foodItem = menu[indexPath.row]
            cell.configCell(food: foodItem)
            return cell

        }else{
            return UITableViewCell()
        }
    }

    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        performSegue(withIdentifier: "popup", sender: menu[indexPath.row])
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if let popupVC = segue.destination as? MenuPopUpVC {
            if let foodItem = sender as? Food{
                popupVC.config(food: foodItem)
            }
        }
    }

    func loadMenu()  {
        ref?.child("menu").observe(.value, with: { (snapshot) in
            for child in snapshot.children {
                let snap = child as! DataSnapshot
                let dict = snap.value as! [String: Any]
                let foodName = dict["name"] as! String
                print(foodName)
                let foodPrice = dict["price"] as! String
                let foodImg = dict["image"] as! String
                let foodItem = Food(name: foodName, price: foodPrice, img: foodImg)

                self.menu.append(foodItem)
            }
        })
    }



}





import UIKit
import SDWebImage

class MenuCell: UITableViewCell {

    @IBOutlet weak var PriceLbl: UILabel!
    @IBOutlet weak var menuImg: UIImageView!
    @IBOutlet weak var menuItemLbl: UILabel!
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

    func configCell(food : Food) {
        let name = food.name ?? ""
        menuItemLbl.text = name
        let price = food.price ?? ""
        PriceLbl.text = "$\(price)"
        menuImg.sd_setImage(with: URL(string: food.img!)) {[weak self] (image, error, cachetype, url) in
            if error == nil{
                self?.menuImg.image = image
            }
        }
    }

}

填充数组后需要重新加载tableView

 self.menu.append(foodItem) 
} 
self.tableView.reloadData()

也在 cellForRowAt 里面,

是个好习惯
let cell = tableView.dequeueReusableCell(withIdentifier: "menuCell") as! MenuCell 

没有误导return UITableViewCell()

您没有重新加载 TableView 的数据,而是在将所有食物附加到 menu 数组后重新加载它们(意味着在 foreach 循环之后)

ref?.child("menu").observe(.value, with: { (snapshot) in
    for child in snapshot.children {
        ...
        self.menu.append(foodItem)
    }
    self.tableView.reloadData()
})