UITableview单元格未显示

时间:2018-12-25 11:11:21

标签: swift uitableview xcode9.4

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
            }
        }
    }

}

2 个答案:

答案 0 :(得分:0)

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

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

也在cellForRowAt内,

let cell = tableView.dequeueReusableCell(withIdentifier: "menuCell") as! MenuCell 

没有误导性的return UITableViewCell()

答案 1 :(得分:0)

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

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