NEW: Start my new Ultimate Portfolio App course with a free Hacking with Swift+ trial! >>

Fixing "Failed to obtain a cell from its DataSource"

Swift version: 5.2

Paul Hudson    @twostraws   

This is a common error, but it's easily fixed. 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: 'UITableView (<UITableView: 0x7f9cd8830c00; frame = (0 0; 414 736); clipsToBounds = YES; autoresize = W+H; gestureRecognizers = <NSArray: 0x7f9cd8430900>; layer = <CALayer: 0x7f9cd8428370>; contentOffset: {0, -64}; contentSize: {414, 0}>) failed to obtain a cell from its dataSource 

…it means that your cellForRowAt method is returning nil for some reason, and it's usually because you are failing to dequeue a reusable cell.

If you want to confirm this, just set a breakpoint after your current dequeue call. 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.description
    return cell
}

…then you should set the breakpoint on the let object = line. If the problem is that tableView.dequeueReusableCell(withIdentifier:) is returning nil, you'll find cell is set to nil.

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)

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")

All being well that should resolve the problem. If not, check that the cell identifier is correct: it's "Cell" by default, but you might have changed it. Such a misspelling ought to cause a crash when tableView.dequeueReusableCell(withIdentifier:) fails, but it's worth checking anyway.

Hacking with Swift is sponsored by ViRE

SPONSORED ViRE offers discoverable way of working with regex. It provides really readable regex experience, code complete & cheat sheet, unit tests, powerful replace system, step-by-step search & replace, regex visual scheme, regex history & playground. ViRE is available on Mac & iPad.

Download on the App Store

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 Swift Design Patterns Buy Testing Swift Buy Hacking with iOS Buy Swift Coding Challenges Buy Swift on Sundays Volume One Buy Server-Side Swift (Vapor Edition) 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 Server-Side Swift (Kitura Edition) Buy Beyond Code

Was this page useful? Let us know!

Average rating: 4.3/5

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.