I have two view controllers, one of them part being a side menu. I try to init its cells with data from an array but I keep having errors when trying to assign a value to a cell label text. Here is the code I have:
class TableViewCell: UITableViewCell {
@IBOutlet weak var cellImage: UIImageView!
@IBOutlet weak var cellLabel: UILabel!
}
class SideMenuTableViewController: UITableViewController {
let vc = ViewController()
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
tableView.reloadData()
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
return vc.array.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TableViewCell
let myImage = UIImage(named: "checkbox")
cell.cellLabel.text = "test" //HERE IS THE ERROR
cell.cellImage.image = myImage
return cell
}
}
And here is the error.
Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value
In the debugger, I can see that my cell contains the label and image from the Sotryboard
but I cannot assign myImage
and any text to the cell.
What am I doing wrong?
Thanks in advance for your help.