János János - 2 months ago 48
iOS Question

UITableview: failed to obtain a cell from its dataSource

I get this error message:

failed to obtain a cell from its dataSource


The strange that

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


not even get called, problem is not that returned cell is nil. I put in breakpoints.
cellForRowAt
not get called.

numberOfRowsInSection
returns value.

I do not know it is relating or not, but now I do not use
UITableViewController
, I just have a
UIViewController
, and it is the
datasource
and
delegate
for the
UITableView
. I hope it can not cause interfere. Any idea?

Answer

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