【问题标题】:Can't make UiTableView work无法使 UiTableView 工作
【发布时间】:2015-04-08 18:57:14
【问题描述】:

我正在开发一个关于 XCode 6 和 Swift 的项目。 我需要将 UITableView 添加到 UIViewController,然后用内容填充表格。

所以,在我的默认 UIViewController 上,我添加了一个 UITableView 和一个 UITableViewCell(使用情节提要)。 接下来,我将 UIViewController 设置为表的数据源和委托。

最后,我将这段代码添加到我的视图 controller.swift 中:

import UIKit

class ViewController: UITableViewController, UITableViewDataSource, UITableViewDelegate {

@IBOutlet var table: UITableView!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


override func numberOfSectionsInTableView(tableView: UITableView) -> Int {

    return 1


}


override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{

    return 3
}



override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

     let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell

    cell.textLabel?.text = "Hello world"

    table.reloadData()

    return cell


}

但是,当我运行该项目时,我遇到了以下日志的崩溃

2015-04-08 21:06:52.820 tableViewTest[12380:783945] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UITableViewController loadView] loaded the "BYZ-38-t0r-view-8bC-Xf-vdC" nib but didn't get a UITableView.'

什么都试过了,真的不知道怎么解决。

谁能帮帮我?

【问题讨论】:

  • cell.textLabel?.text = "Hello world" table.reloadData() --> 你不必重新加载,我猜这会导致递归调用并因此崩溃
  • 我试过了,但实际上它还是崩溃了。 @维格
  • 你的控制台输出是什么
  • 2015-04-08 21:06:52.820 tableViewTest[12380:783945] *** 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:'-[UITableViewController loadView] 加载了“BYZ- 38-t0r-view-8bC-Xf-vdC" nib 但没有得到 UITableView。 @维格
  • 该错误表明您有一个 UITableViewController(这也是您的代码所说的),但您告诉我们您正在使用 UIViewController。您似乎对自己实际在做什么感到困惑。

标签: ios uitableview swift ios8 xcode6


【解决方案1】:

您有一个IBOutlet 的表格视图,并且您需要您的ViewControllerUIViewController 的子类。

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {}

【讨论】:

  • 它不应该“符合” UIViewController; UIViewController 不是协议。它应该是“你需要 ViewController 的超类是 UIViewController”。
  • 啊,是的,我怎么错过了
【解决方案2】:

在我看来,您在情节提要中使用了UIViewController,但在代码中引用了UITableViewController。尝试改变

class ViewController: UITableViewController, UITableViewDataSource, UITableViewDelegate {

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

并将table.reloadData() 放入viewDidLoad 函数中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-08-18
    • 2019-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多