gpt4 book ai didi

ios - 无法让 UITableViewController 动态填充

转载 作者:行者123 更新时间:2023-11-28 11:21:16 24 4
gpt4 key购买 nike

我正在学习一些关于如何填充表格 View 的基本教程。我在 UITableView 和动态填充方面取得了成功,但遵循相同的步骤对 UITableViewController 不适用。

关于为什么我在使用 Controller 时遇到困难的任何解释?

关于 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] * Assertion failure in -[UITableView dequeueReusableCellWithIdentifier:forIndexPath:], /SourceCache/UIKit_Sim/UIKit-3318.16.14/UITableView.m:6116 2015-01-11 18:30:11.560 TableViewTest[30788:8219862] * Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'unable to dequeue a cell with identifier reuseIdentifier - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'

最佳答案

根据您的错误消息,这是解决方案:

在 ViewDidLoad() 中,添加:

self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "reuseIdentifier")

在这里查看我的示例项目:https://github.com/ericcgu/EGSwiftTableViewController

关于ios - 无法让 UITableViewController 动态填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27894286/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com