Swift version: 5.6
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.
SAVE 50% To celebrate WWDC23, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.
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.