UITableview: failed to obtain a cell from its dataSource

蓝咒 提交于 2019-12-24 07:38:29

问题


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. cellForRowAtnot 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?


回答1:


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




回答2:


I guess this is your problem - Failed to obtain a cell from its DataSource

you can take a look at: https://www.hackingwithswift.com/example-code/uikit/fixing-failed-to-obtain-a-cell-from-its-datasource

try something like this:

cell = tableView.dequeueReusableCellWithIdentifier(getCellIdentifierForType(cellType))
        setDataToCell(cell, item: item)

good luck



来源:https://stackoverflow.com/questions/39902614/uitableview-failed-to-obtain-a-cell-from-its-datasource

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!