This issue is driving me crazy for 3 days. I created a custom cell class:
class TransitoPalinaTableViewCell: UITableViewCell {
@IBOutlet weak var lblOrario: UILabel!
@IBOutlet weak var lblDestinazione: UILabel!
@IBOutlet weak var lblNote: UILabel!
@IBOutlet weak var lblBus: 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
}
}
and in my view controller I have this:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! TransitoPalinaTableViewCell
// Configure the cell...
let hour = Int(self.transito.listaCorse[indexPath.row].tempoTransito) / 3600
let mins = Int(self.transito.listaCorse[indexPath.row].tempoTransito % 3600) / 60
cell.lblOrario?.text = String(format: "%02d:%02d", hour, mins)
cell.lblDestinazione?.text = self.transito.listaCorse[indexPath.row].arrivoCorsa
cell.lblNote?.text = ""
cell.lblBus?.text = self.transito.listaCorse[indexPath.row].automezzo
cell.backgroundColor = UIColor.init(red: 0, green: 60/255, blue: 113/255, alpha: 1)
return cell
}
In the storyboard I set the table view cell Identifier: "Cell" and Custom Class "TransitoPalinaTableViewCell".
The problem is that when cellForRowAt is called I got the error
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'unable to dequeue a cell with identifier Cell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'
at the line of dequeueReusableCell command.
As I read in other posts I didn't put the line
self.tableView.register(TransitoPalinaTableViewCell.self, forCellReuseIdentifier: "Cell")
in the viewdidLoad() - it'd create a brand new cell model, not connected with the outlets in the storyboard.
I tried to create again the TransitoPalinaTableViewCell class and its outlets but nothing changed.
Any help appreciated.
You can register a cell with either cell class or cell nib.
tableView.register(UINib(name: "YourNibName", bundle: nil), forCellReuseIdentifier: "Cell")
but that works only if you created a cell in Xib file. If it's created in Storyboard, then I'm afraid it's not possible to use that cell anywhere else than the ViewController you created it in.