我绞尽脑汁试图弄清楚这是如何工作的,但我似乎无法理解。我尝试过使用其他教程,但随着许多测试版的发布,一切都在不断变化。我对 IOS 开发相当陌生,所以我有点挣扎。
在 Storyboard 中,我有 UITableView,其中包含一个标识符为“myCell”的单元格。
这是我到目前为止所拥有的。当我运行 IOS 模拟器时,表格 View 上没有显示任何内容。
关于如何解决这个问题有什么建议吗?
class ViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("myCell", forIndexPath: indexPath) as UITableViewCell
cell.textLabel?.text = "Cell #: \(indexPath.row)" // display the row number
return cell
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10; // testing out with 10 cells
}
}
最佳答案
添加功能
optional func numberOfSectionsInTableView(tableView: UITableView) -> Int
并返回您想要的部分数量。
您应该确保在 Storyboard中您的 UITableViewController 具有 ViewController 类,如下所示:
ViewController 既是 UITableViewController 的委托(delegate)又是数据源,如下所示(引用 Outlet):
如果您根本没有看到任何行(请检查底部的行),您还应该检查您的 UITableViewController 是否设置为initialViewController。
关于ios - 如何使用 Swift 向 UITableView 添加项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25674892/