【发布时间】:2015-01-12 01:51:18
【问题描述】:
我正在学习一些关于如何填充表格视图的基本教程。我在 UITableView 和动态填充方面取得了成功,但是对于 UITableViewController 来说,遵循相同的步骤对我不起作用。
任何解释为什么我在使用控制器时遇到困难?
UITableViewController 上的信息似乎没有 UITableView 那么多。为什么每个人似乎都倾向于只使用 UITableView?
为了记录,这是我正在关注的教程:http://www.ioscreator.com/tutorials/tableview-tutorial-in-ios8-with-swift
import UIKit
class TestTableViewController: UITableViewController, UITableViewDelegate {
let tableData = ["One", "Two", "Three"]
override func viewDidLoad() {
super.viewDidLoad()
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Potentially incomplete method implementation.
// Return the number of sections.
return 0
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete method implementation.
// Return the number of rows in the section.
return countElements(tableData)
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath) as UITableViewCell
// Configure the cell...
cell.textLabel?.text = tableData[indexPath.row]
return cell
}
/*
// Override to support conditional editing of the table view.
override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
// Return NO if you do not want the specified item to be editable.
return true
}
*/
/*
// Override to support editing the table view.
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == .Delete {
// Delete the row from the data source
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
} else if editingStyle == .Insert {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
*/
/*
// Override to support rearranging the table view.
override func tableView(tableView: UITableView, moveRowAtIndexPath fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath) {
}
*/
/*
// Override to support conditional rearranging of the table view.
override func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool {
// Return NO if you do not want the item to be re-orderable.
return true
}
*/
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
}
将 numberOfSectionsInTableView 更改为返回 1 后,这是我收到的错误。
2015-01-11 18:30:11.557 TableViewTest[30788:8219862] * 断言 -[UITableView 中的失败 dequeueReusableCellWithIdentifier:forIndexPath:], /SourceCache/UIKit_Sim/UIKit-3318.16.14/UITableView.m:6116 2015-01-11 18:30:11.560 TableViewTest[30788:8219862] * 终止应用程序由于 未捕获的异常“NSInternalInconsistencyException”,原因:“无法 使用标识符reuseIdentifier出列一个单元格 - 必须注册一个 nib 或一个类作为标识符或连接一个原型单元 故事板'
【问题讨论】:
-
您能否为您的表格视图方法添加代码,以便我们看到您在做什么?
-
刚刚添加。其他人添加了一个答案(似乎已被删除)建议添加 UITableViewDelegate。但是,当我运行时仍然没有填充。
-
为您的表格视图设置委托和数据源。我认为你应该在
numberOfSectionsInTableView方法中返回 1 -
delegate 和 dataSource 都连接到视图控制器。在 numberOfSectionsInTableView 方法中输入 1 并出现错误。进一步调查错误。看起来它在抱怨出队调用中使用的“reuseIdentifier”。
标签: ios uitableview swift