问题
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