ios - Swift - TableView 加载错误 - 可怕的 SIGBART

标签 ios swift uitableview

从头开始一个 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/

相关文章:

ios - AWS Lambda swift SDK 对用户池进行身份验证

animation - 快速访问函数内部闭包的变量

iOS - 无法调用非函数类型的值 'NSProgress'

swift - 使用 NSUSerDefaults 保存 Swift TableView 数据

ios - SWIFT:来自 TableViewCell(原型(prototype)单元格)的模态 Segue 不工作

iphone - 更改 Xcode 4 用于属性列表的编辑器?

javascript - 我如何请求增加 iPad 上的 HTML5 localstorage 大小,就像 FT 网络应用程序一样?

ios - 当 iOS 终止一个应用程序时,当应用程序再次打开时哪个 ViewController 会启动?

ios - 我试图在覆盖 func tableView(...didSelectRowAt...) 时关闭我的 tableViewController,但没有任何反应

objective-c - 在表格 View 中显示特定大小的两个部分