无法使 UiTableView 工作
Can't make UiTableView work
我正在 XCode 6 和 Swift 上做一个项目。
我需要将 UITableView 添加到 UIViewController,然后用内容填充 table。
因此,在我的默认 UIViewController 上,我添加了一个 UITableView 和一个 UITableViewCell(使用故事板)。
接下来,我将 UIViewController 设置为 table 的数据源和委托。
最后,我将此代码添加到我的视图中 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.'
什么都试过了,实在不知道怎么解决。
谁能帮帮我?
在我看来,您在故事板中使用了 UIViewController
,但在代码中引用了 UITableViewController
。尝试改变
class ViewController: UITableViewController, UITableViewDataSource, UITableViewDelegate {
至
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
并在 viewDidLoad
函数中放入 table.reloadData()
。
您有一个表视图 IBOutlet
,您需要 ViewController
成为 UIViewController
的子类。
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {}
我正在 XCode 6 和 Swift 上做一个项目。 我需要将 UITableView 添加到 UIViewController,然后用内容填充 table。
因此,在我的默认 UIViewController 上,我添加了一个 UITableView 和一个 UITableViewCell(使用故事板)。 接下来,我将 UIViewController 设置为 table 的数据源和委托。
最后,我将此代码添加到我的视图中 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.'
什么都试过了,实在不知道怎么解决。
谁能帮帮我?
在我看来,您在故事板中使用了 UIViewController
,但在代码中引用了 UITableViewController
。尝试改变
class ViewController: UITableViewController, UITableViewDataSource, UITableViewDelegate {
至
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
并在 viewDidLoad
函数中放入 table.reloadData()
。
您有一个表视图 IBOutlet
,您需要 ViewController
成为 UIViewController
的子类。
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {}