2016-09-19 98 views
0

我更新了Xcode,此后我的数据库出现问题。 代码:'UITableView'未能从其dataSource获取单元格

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 
} 

错误,我得到的是:

终止应用程序由于未捕获的异常 'NSInternalInconsistencyException',原因:“的UITableView(;层=; contentOffset:{0,-64} ; contentSize:{375,65802}>)未能从其DataSource()

+0

cellForRowAtIndexPath返回零 –

回答

0

您的cellForRow方法名是不正确的。

替换该行

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

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

它解决了它,太棒了,谢谢! ! –

1

添加到您viewDidLoad方法获得细胞:

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

这是假设你在故事板的tableView中没有原型单元格。如果您确实有原型单元格,请确保重用标识符为"Cell",单元格的自定义类别为LeadsTableViewCell;那么就不需要注册该单元。

UPDATE

我只注意到你的方法名是错误的,它应该是:

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 
} 
+0

仍然是一样的错误 –

+0

我的故事板中有一个原型“Cell”,我保证你所说的一切,但是我仍然得到相同的错误。谢谢 –

+0

如果有帮助,线程停在班级AppDelegate –

0

为了得到你的表视图中的单元格,你必须实现旨在提供表视图与单元格中的数据源方法对于索引路径中的给定行。此方法是:

tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) 

由于此方法是一种必需的方法,因此您不能将其忽略出来,否则会出现错误。用这种方法配置你的单元格。