Search code examples
swiftuitableviewxcode9.4

UITableview cells aren't showing


UITableview is visible while the cells aren't.

This is for a food ordering app, and I'm trying to display the menu. I've tried everything, no error has shown, but the cells ain't visible

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

}

Solution

  • You don't reload data of your TableView, reload them after you append all foods to menu array (means after foreach loop)

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