0

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.

cicaletto79
  • 179
  • 1
  • 12
  • Probably obviously yes, but just to rule out reasons... In your Storyboard, did you design your cell as a Prototype cell in the table view? – DonMag Oct 05 '17 at 14:03
  • This link can be helpful https://stackoverflow.com/questions/540345/how-do-you-load-custom-uitableviewcells-from-xib-files – Maulik Bhuptani Oct 05 '17 at 14:05
  • @DonMag yes I did. In my storyboard I dropped my objects inside my cell and set 1 Dynamic Prototype cell in the table. – cicaletto79 Oct 05 '17 at 14:08
  • @cicaletto79 - again, just to rule out possible causes... you put "Cell" (no quotes, case-sensitive) in the Identifier field under Attributes inspector as shown here: https://imgur.com/A0LjMjz – DonMag Oct 05 '17 at 14:20
  • @cicaletto79 have you given correct class name and cell identifier in storyboard?? – Tushar Sharma Oct 05 '17 at 14:26
  • And.... in Storyboard, your Prototype cell ***IS*** in the same TableView that you are using? So, you don't have multiple UITableView controllers where you are trying to use the same Prototype cell? – DonMag Oct 05 '17 at 14:26
  • I solved this issue tanks to @mag_zbc . His answer below and how I worked to solve my problem following. – cicaletto79 Oct 05 '17 at 14:59

2 Answers2

1

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.

mag_zbc
  • 6,801
  • 14
  • 40
  • 62
  • I tried your suggestion by putting let cellNib = UINib.init(nibName: "Cell", bundle: nil) self.tableView.register(cellNib, forCellReuseIdentifier: "Cell") in my viewDidLoad but I got the error "Could not load NIB in bundle" – cicaletto79 Oct 05 '17 at 14:05
  • Like I wrote - that works if the cell is created in Xib, not in Storyboard. Is your cell in Xib or in Storyboard? – mag_zbc Oct 05 '17 at 14:08
0

I created a new CustomTableViewCell class with .xib file and used this file to connect outlets with the class. I set customCell as identifier in the Attributes Inspector of the xib file and removed any other identifier from storyboard in the table view controller.

In the viewDidLoad I registered the Cell like this:

let cellNib = UINib.init(nibName: "CustomTableViewCell", bundle: nil)
self.tableView.register(cellNib, forCellReuseIdentifier: "customCell")

and in the cellForRowAt I used

let cell = tableView.dequeueReusableCell(withIdentifier: "customCell", for: indexPath) as! CustomTableViewCell
cicaletto79
  • 179
  • 1
  • 12