简体   繁体   中英

Swift 3: Custom TableView Cell not showing

hey so I'm trying to create a custom cell and I've properly linked everything in my story board but for some reason it doesn't display anything when I run. I Tried testing it out, it appears that the custom cell objects don't even get created.

 class customCell : UITableViewCell{

    @IBOutlet weak var EventImage: UIImageView!
    @IBOutlet weak var type: UILabel!
    @IBOutlet weak var rating: UIImageView!
    @IBOutlet weak var date: UILabel!
    @IBOutlet weak var name: UILabel!
}


class EventHistoryViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet weak var TableView: UITableView!

    let k = ["hi","hey","yo","hello"]

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

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return k.count
    }

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

        let cell = self.TableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! customCell

    cell.name!.text = self.k[index.row]

    return cell


}


override func viewDidLoad() {
    super.viewDidLoad()

    self.TableView.register(customCell.self, forCellReuseIdentifier: "cell")

    TableView.delegate = self
    TableView.dataSource = self

}

what am I doing wrong? the program in this case crashes when it reaches cell.name!.text = self.k[index.row]

In place of cell.name!.text write cell.name.text

Don't register the cell

Check this one

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell: VC_FavouriteCell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath as IndexPath) as! VC_FavouriteCell

        cell.name.text = k[index.row]

    return cell
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM