1

I'm trying to create a simple app, but:

'NSInternalInconsistencyException', reason: 'unable to dequeue a cell with identifier term - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'

Code:

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

    cell.textLabel?.text = titles[indexPath.row]

    return cell
}

Screens:

class

identifier

I have no idea what to do.

Nazmul Hasan
  • 10,130
  • 7
  • 50
  • 73
nslllava
  • 589
  • 2
  • 9
  • 19
  • 1
    Your cell is connected but where is your tableview setup? Is it in code or storyboard. If its in code you need to `tableView.registerCell(_:)` – Ryan Poolos Apr 13 '17 at 20:04
  • Or if your cell is a nib you'll need to register that nib as well. – Ryan Poolos Apr 13 '17 at 20:12
  • Possible duplicate of [Assertion failure in dequeueReusableCellWithIdentifier:forIndexPath:](http://stackoverflow.com/questions/12737860/assertion-failure-in-dequeuereusablecellwithidentifierforindexpath) – Nazmul Hasan Apr 13 '17 at 20:13
  • My cell is not a nib. When I add tableView.register(CostumeCellView.self, forCellReuseIdentifier: "Cell"), my outlets is nil. – nslllava Apr 13 '17 at 20:15
  • Sounds like you're using a Storyboard then with prototype cells. A screenshot of your tableView would help. But I'll update my answer with some info about that – Ryan Poolos Apr 13 '17 at 20:16
  • Do you mean this http://imgur.com/a/HYzCo or Storyboard? – nslllava Apr 13 '17 at 20:21
  • That looks like the prototype cell is setup properly. So the problem might be that the tableView isn't hooked up right or your custom class isn't set right. So the tableView in your method isn't actually the tableView from your storyboard. – Ryan Poolos Apr 13 '17 at 20:22
  • One more question: can it happen because I show the TableViewController in this way: storyboard?.instantiateViewController and then navigationController?.pushViewController ? – nslllava Apr 13 '17 at 20:31
  • @czekhoff no way to occur for this `storyboard?.instantiateViewController and then navigationController?.pushViewController` – Nazmul Hasan Apr 13 '17 at 20:35
  • @czekhoff clean and build then try to run . if failed try with new one create – Nazmul Hasan Apr 13 '17 at 20:36
  • If you could show more screenshots of your storyboard setup and of the code for your tableView I think we could track down the cause. Something is likely not hooked up just right, its just not your cell. Thats hooked up fine :) – Ryan Poolos Apr 13 '17 at 21:21

2 Answers2

5

You probably need to register your cell. It would look something like this.

If you're using a cell class in code:

tableView.register(MyTableViewCell.self, forCellReuseIdentifier: "Cell")

If you're using a cell with a nib:

let nib = UINib(nibName: "MyTableViewCellNibFile", bundle: nil)
tableView.register(nib, forCellReuseIdentifier: "Cell")

If you're using a storyboard you need to make sure your prototype cell is setup properly. This answer actually has that defined well https://stackoverflow.com/a/14939860/563381

https://developer.apple.com/reference/uikit/uitableview/1614937-register

Community
  • 1
  • 1
Ryan Poolos
  • 18,421
  • 4
  • 65
  • 98
0

I hope you are trying to load the prototype cell from the XIB.As the method name implies, dequeue method, just dequeue the cell, if it is already created and available in memory. You have to create your cell when dequeue is not finding the one for you, Get along with the below approach,This is what i usually do for loading the prototype cell,You dont need to do registerclass/registernib here,

var cell = tableView.dequeueReusableCellWithIdentifier("Cell") as? CostumeCellView

  if cell == nil {
        let topLevelObjects = NSBundle.mainBundle().loadNibNamed("MyCellNibFile", owner: self, options: nil)
        for entry in topLevelObjects {
            if entry.reuseIdentifier == "Cell" {
                cell = entry as? CostumeCellView
            }
        }
    } 
cell.textLabel?.text = titles[indexPath.row]

return cell
Prabhu.Somasundaram
  • 1,380
  • 10
  • 13