user25093
user25093

Reputation: 287

Data not showing up in UITableView

class TableController: UIViewController {

    @IBOutlet var ListTable: UITableView!

    var list: [Dictionary<String, String>] = []

    override func viewDidLoad() {
        super.viewDidLoad()

        let ListTable = UITableView(frame: view.bounds)
        self.ListTable = ListTable

        ListTable.dataSource = self
        ListTable.delegate = self

        initList()
    }


    func initList() {
          // get list from firebase
            self.ListTable.reloadData()
    }

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

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let item = self.list[indexPath.row]
        let cellIdentifier = "ListCell"

        let cell = CustomCell(style: .default, reuseIdentifier: cellIdentifier)

        cell.foodLabel?.text = item["Banana"]

        return cell
    }
}

extension QueueController: UITableViewDataSource, UITableViewDelegate {

}

CustomCell class:

import UIKit

class CustomCell: UITableViewCell
{

    @IBOutlet weak var foodLabel: UILabel!

    override func awakeFromNib()
    {
        super.awakeFromNib()
    }
}

My data from firebase loads properly. On storyboard I have a normal view controller with a UITableView embedded inside of it. That table view is liked to my IBOutlet for my ListTable. In the table there is a cell with 3 labels. That cell has the identifier ListCell and it's class is CustomCell.

Edit: There is no error but my data isn't showing up.

Upvotes: 0

Views: 636

Answers (3)

CZ54
CZ54

Reputation: 5588

You did never add the TableView to your view... (or part go the code is missing )

Upvotes: 0

Usman Javed
Usman Javed

Reputation: 2455

This is because your Custom Cell does not dequeue properly. Try this one

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cellIdentifier = "ListCell"
        var cell : ListCell? = tableView.dequeueReusableCell(withIdentifier: cellIdentifier) as! ListCell?
        if (cell == nil) {

            cell = Bundle.main.loadNibNamed("ListCell", owner: nil, options: nil)?[0] as? ListCell
        }


        cell?.backgroundColor = UIColor.clear
        cell?.contentView.backgroundColor = UIColor.clear

        return cell!
    }

Upvotes: 1

Benjamin Lowry
Benjamin Lowry

Reputation: 3789

Perhaps try registering your cell in viewDidLoad

ListTable.register(UINib(nibName: "CustomCell", bundle: Bundle.main), forCellReuseIdentifier: "ListCell") //this is assuming that your nib is named "CustomCell"

Also, for the record, you should follow camel-case conventions and name your UITableView listTable

Upvotes: 1

Related Questions