1

I am getting this error:

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

I am using two cells with the same class "NavigationItemCell" and two identifiers "NavigationNodeCell" and "NavigationLinkCell".

And I create a cell with e.g.:

let cell: NavigationItemCell  = self.tableView.dequeueReusableCellWithIdentifier("NavigationNodeCell",     forIndexPath: indexPath) as! NavigationItemCell

This problem has been there before me, e.g. Assertion failure in dequeueReusableCellWithIdentifier:forIndexPath:

As I understand (e.g. the anser of Sebastian Borggrewe) it should be enough to register a UITableViewCell in the story board with its custom class and an identifier.

That's exactly what I did and I am still getting this error. I tried to use two different classes but it did not solve the error. I also tried to register my cells with nib but there I am running into other issues (label is nil).

I've found that this problem probably occures because I instanciate a new table view controller programmatically.

var newController = MyTableViewController()

Does it mean I will have to register nibs anyway? Is it a problem that I will have to register the same class twice? And how do I avoid the problem of the nil label?

Community
  • 1
  • 1
Nina
  • 681
  • 1
  • 10
  • 27
  • Other than the answers already posted, also make sure that your reuseIdentifier in cellForRowAtIndexPath is same as defined while registering the cellClass/nib with the tableView and the one defined in storyboard/xib of the cell. – NSNoob Aug 30 '16 at 07:31

2 Answers2

5

You should create an instance of your view controller with storyboard ID. Replace the line var newController = MyTableViewController() with the following code

let storyboard = UIStoryboard(name: "YOUR_STORYBOARD_NAME", bundle: nil)
let newController = storyboard.instantiateViewController(withIdentifier: "VIEWCONTROLLER_ID_FROM_STORYBOARD")

Edit: It's also possible to use: self.storyboard!.instantiateViewControllerWithIdentifier("VIEWCONTROLLER_ID_FROM_STORYBOARD")

if the view controller is instantiated from the storyboard, and second view controller is in the same storyboard

Tiko
  • 485
  • 4
  • 10
  • Tried it, I am getting the error: 'storyBoardWithName(_:bundle:)_ is unavailable: use object construction 'UIStoryBoard(name:bundle:)' – Nina Aug 29 '16 at 20:40
  • I'm sorry, I have translated code from objective-c, try this one `UIStoryboard(name: "MyStoryboardName", bundle: nil)` – Tiko Aug 29 '16 at 20:43
  • I tried "let controller = self.storyboard!.instantiateViewControllerWithIdentifier("ID") ... this seems to work, but I am not sure yet what the ID of my UITableViewController is. – Nina Aug 29 '16 at 20:45
  • http://stackoverflow.com/questions/13867565/what-is-a-storyboard-id-and-how-can-i-use-this – Tiko Aug 29 '16 at 20:47
  • Thanks! That seems to work. It's not yet doing what it should do, but at least the error is gone! – Nina Aug 29 '16 at 20:50
  • You're welcome, if my answer helped You, mark the answer as correct)) – Tiko Aug 29 '16 at 20:56
  • In Swift 3 the method is called "instantiateViewController" not "instantiateViewControllerWithIdentifier". – Nina Sep 25 '16 at 20:08
1

If you create your table view controller with an initializer (instead of storybaord.instantiateViewController(withIdentifier:)), then your table view controller will not know about the cells you put in the storyboard. If you want to use an initializer, then you will need to manually register the custom cell class in your controller's viewDidLoad.

class MyTableViewController: UITableViewController {

    override func viewDidLoad() {
        super.viewDidLoad
        tableView.register(NavigationNodeCell.self, forCellReuseIdentifier: "NavigationNodeCell")
    }

}
keithbhunter
  • 12,258
  • 4
  • 33
  • 58