Charles Thao
Charles Thao

Reputation: 13

TableView displaying empty cells

As titled, I'm trying to display some texts in labels linked to a Controller but couldn't display anything but blank cells. Every labels are linked to the controller properly but the cells are still empty. Is there something wrong with the code?

import UIKit

class TableViewCell: UITableViewCell{

    @IBOutlet weak var label1: UILabel!
    @IBOutlet weak var label2: UILabel!
    @IBOutlet weak var Date: UILabel!
}
class Controller: UIViewController, UITableViewDataSource,UITableViewDelegate {

    let textCellIdentifier = "ShowCell"

    @IBOutlet weak var tableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()
        self.tableView.delegate = self
        self.tableView.dataSource? = self
       tableView.register(UITableViewCell.self, forCellReuseIdentifier: "ShowCell")
        LeagueData.single.addListener {
            self.tableView.reloadData()
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
   func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

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

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "ShowCell") as! TableViewCell!
        cell?.label1?.text = "sample text" //LeagueData.single.schedule[0].team1.name
        cell?.label2.text = LeagueData.single.schedule[0].team2.name
        cell?.Date.text = LeagueData.single.schedule[0].date
        print(LeagueData.single.schedule[0].date)
        return cell!
    }
}

Upvotes: 1

Views: 751

Answers (1)

weikel
weikel

Reputation: 164

tableView.register(UITableViewCell.self, forCellReuseIdentifier: "ShowCell")

change to this

tableView.register(TableViewCell.self, forCellReuseIdentifier: "ShowCell")

or if You use xib, change to

tableView.registerNib(UINib(nibName: "TableViewCell", bundle: nil), forCellReuseIdentifier: "ShowCell")

Upvotes: 1

Related Questions