Failed to Obtain a Cell from Its Datasource with Swift 3

Failed to obtain a cell from its DataSource

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.

failed to obtain a cell from its dataSource with swift 3

The error

failed to obtain a cell from its dataSource

occurs because the signature of cellForRowAtIndexPath is wrong. In Swift 3 it's

(override) func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell

and use the convenience method

let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for:indexPath)

which returns always a valid non-optional cell.


PS: You don't need to set datasource and delegate to self because UITableViewController does that implicitly

UITableview: failed to obtain a cell from its dataSource

Cross check below check list:-

  • it's because you are failing to dequeue a reusable cell.

    • The problem is that your cellForRowAtIndexPath function is embedded in another function
    • when you forgot to add the UITableViewDataSource and UITableViewDelegate protocols to the ViewController declaration.

class DataViewController: UIViewController, UITableViewDataSource, UITableViewDelegate

failed to obtain a cell from its dataSource for swift4.2

Can you print weeklyWeather.count to see its number?

You may also peek the cell by setting a breakpoint at beginning of your tableView: cellForRowAt method.

Hope it helps.

UITableView' failed to obtain a cell from its dataSource

Your method name for cellForRow is not correct.

Replace this line

func cellForRow(at indexPath: IndexPath) -> UITableViewCell?
{

with

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {


Related Topics



Leave a reply



Submit