'UITableView' failed to obtain a cell from

2019-03-06 11:20发布

I updated Xcode and since then I'v problems with my dataBase. code:

override func numberOfSections(in tableView: UITableView) -> Int {
    // return the number of sections
    return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
    // return the number of rows
    return leadItems.count
}

func cellForRow(at indexPath: IndexPath) -> UITableViewCell?
{
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! LeadsTableViewCell

    // Configure the cell...
    cell.user_name_label.text = leadItems[indexPath.row].user_name
    cell.school_name_label.text = leadItems[indexPath.row].school_name

    return cell
}

The error that I get is:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView (; layer = ; contentOffset: {0, -64}; contentSize: {375, 65802}>) failed to obtain a cell from its dataSource ()

3条回答
你好瞎i
2楼-- · 2019-03-06 11:32

In order to get a cell for your table view you must implement the data source method that is designed to provide the table view with a cell for a given row at an index path. This method is:

tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath)

Because this method is a required method you can not leave it out or you will get an error. Configure your cell in this method.

查看更多
\"骚年 ilove
3楼-- · 2019-03-06 11:38

Add this to your viewDidLoad method:

tableView.register(LeadsTableViewCell.self, forCellReuseIdentifier: "Cell")

This is assuming you don't have a prototype cell in the tableView in your storyboard. If you do have a prototype cell, ensure the Reuse Identifier is "Cell" and the custom class of the cell is LeadsTableViewCell; then there'll be no need to register the cell.

UPDATE

I just noticed your method name is wrong, it should be:

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

    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! LeadsTableViewCell

    // Configure the cell...
    cell.user_name_label.text = leadItems[indexPath.row].user_name
    cell.school_name_label.text = leadItems[indexPath.row].school_name

    return cell
}
查看更多
不美不萌又怎样
4楼-- · 2019-03-06 11:45

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 {
查看更多
登录 后发表回答