Failed to obtain a cell from its DataSource

Using Swift 3 I encountered this error when I forgot to add the UITableViewDataSource and UITableViewDelegate protocols to the ViewController declaration.

class DataViewController: UIViewController, UITableViewDataSource, UITableViewDelegate

Adding them fixed the problem.


Swift 3.0

You just need to add UITableViewDelegate and UITableViewDataSource

import UIKit

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource

Below image is just for your reference.

enter image description here


Your error suggests that cellForRowAtIndexPath is returning nil for some reason, and I'm guessing it's because you are failing to dequeue a reusable cell. If you want to confirm this, just set a breakpoint after your current dequeue call: I expect 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:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

If you aren't using an Xcode template, use that line of code anyway then register your own re-use identifier like this:

[self.tableView registerClass:[UITableViewCell self] forCellReuseIdentifier:@"Cell"];

All being well that should resolve the problem. I wrote this up in more detail for Swift users.