NEW: Get your ticket for Hacking with Swift Live 2019! >>

Fixing "Failed to obtain a cell from its DataSource"

Swift version: 5.0

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.

SPONSOR Tired of wasting time debugging your Swift app? Instabug’s SDK is here to help you minimize your debugging time by providing you with complete device details, network logs, and reproduction steps with every bug report. All data is attached automatically. It only takes a line of code to setup. Get started for free.

Available from iOS 6.0

Did this solution work for you? Please pass it on!

Other people are reading…

About the Swift Knowledge Base

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

MASTER SWIFT NOW
Buy Testing Swift Buy Practical iOS 12 Buy Pro Swift Buy Swift Design Patterns Buy Swift Coding Challenges Buy Server-Side Swift (Vapor Edition) Buy Server-Side Swift (Kitura Edition) Buy Hacking with macOS Buy Advanced iOS Volume One Buy Advanced iOS Volume Two Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with Swift Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Beyond Code

Was this page useful? Let me know!

Average rating: 4.0/5

Click here to visit the Hacking with Swift store >>