从头开始一个 Xcode 项目。选定的单一 View 选项。放在 View 上方的 TableView 中。选择了1个细胞原型(prototype)。创建了一个单元标识符“cell”。添加了 UITableViewDelegate 和满足 tableview 协议(protocol)所需的功能。 TableView 链接到 View Controller 。该代码没有预运行错误或警告。但是,我收到此错误:
2016-10-15 07:50:29.622 LawDataNHC[5219:196567] * Assertion failure in -[UITableView _configureCellForDisplay:forIndexPath:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3599.6/UITableView.m:8035 2016-10-15 07:50:29.650 LawDataNHC[5219:196567] * Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView (; layer = ; contentOffset: {0, 0}; contentSize: {375, 44}>) failed to obtain a cell from its dataSource ()' *** First throw call stack:
Material TableView 代码:
class ViewController: UIViewController, UITableViewDelegate {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
private func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: .default, reuseIdentifier: "cell")
cell.textLabel?.text = "test"
return cell
}
最佳答案
您忘记在 ViewController
中命名协议(protocol) UITableViewDataSource
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
此外,您的 cellForRowAt
方法不应该是私有(private)的:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
最后尝试使用 dequeueReusableCell
方法生成单元格,而不是使用 UITableViewCell(style: .default, reuseIdentifier: "cell")
创建新单元格。
您的 ViewController
类应如下所示:
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
//let cell = UITableViewCell(style: .default, reuseIdentifier: "cell")
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = "test"
return cell
}
}
关于ios - Swift - TableView 加载错误 - 可怕的 SIGBART,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40058811/