Swift version: 5.10
This error usually means there's a problem with your cell prototypes. There are two main reasons why table views fail to return cells, but they give different error messages. If you get an error like this:
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'
…it means that your call to dequeueReusableCell(withIdentifier:)
is failing, which is usually caused by having no prototype cells with the identifier you requested.
First: check that you have a prototype cell registered. By default you should have one in the storyboard, but if you created your own table view then you might have moved things around. You might also have registered one in code.
Second: check that your spelling of the identified is correct. It's "Cell" by default, in the code and in the storyboard, and these two things need to match in order for everything to work.
You can verify the error by placing a breakpoint in your cellForRowAtIndexPath
method. For example, if you have code like this:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell")!
let object = objects[indexPath.row]
cell.textLabel?.text = object
return cell
}
…then you should set the breakpoint on the let object =
line. If the problem is that tableView.dequeueReusableCell(withIdentifier:)
is failing, your breakpoint won't be hit.
If you're using modern Xcode templates where you get a prototype cell made for you, you should probably be using this instead:
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
You should then ensure a prototype cell exists in your tableview with that identifier – double check the name, and make sure you've typed it into the "Identifier" box and not "Class" or something else.
If you aren't using an Xcode template, use that line of code anyway then register your own re-use identifier like this:
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")
SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until February 9th.
Sponsor Hacking with Swift and reach the world's largest Swift community!
Available from iOS 6.0
This is part of the Swift Knowledge Base, a free, searchable collection of solutions for common iOS questions.
Link copied to your pasteboard.