GO FURTHER, FASTER: Try the Swift Career Accelerator today! >>

Fixing "Unable to dequeue a cell with identifier"

Swift version: 5.10

Paul Hudson    @twostraws   

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")
Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure and A/B test your entire paywall UI without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Available from iOS 6.0

Similar solutions…

About the Swift Knowledge Base

This is part of the Swift Knowledge Base, a free, searchable collection of solutions for common iOS questions.

BUY OUR BOOKS
Buy Pro Swift Buy Pro SwiftUI Buy Swift Design Patterns Buy Testing Swift Buy Hacking with iOS Buy Swift Coding Challenges Buy Swift on Sundays Volume One Buy Server-Side Swift Buy Advanced iOS Volume One Buy Advanced iOS Volume Two Buy Advanced iOS Volume Three Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with macOS Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Beyond Code

Was this page useful? Let us know!

Average rating: 3.9/5

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.