NEW: Master Swift design patterns with my latest book! >>

Fixing "Failed to obtain a cell from its DataSource"

Written by 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.

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.

Get my latest video for free

Learn about value types, functional programming, and protocol-oriented programming in this new video – it's free!

Click here to visit the Hacking with Swift store >>