【发布时间】: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