ios - 如何使用 Swift 向 UITableView 添加项目?

标签 ios uitableview swift

我绞尽脑汁试图弄清楚这是如何工作的,但我似乎无法理解。我尝试过使用其他教程,但随着许多测试版的发布,一切都在不断变化。我对 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 类,如下所示:

enter image description here

ViewController 既是 UITableViewController 的委托(delegate)又是数据源,如下所示(引用 Outlet):

enter image description here

如果您根本没有看到任何行(请检查底部的行),您还应该检查您的 UITableViewController 是否设置为initialViewController。

enter image description here

关于ios - 如何使用 Swift 向 UITableView 添加项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25674892/

相关文章:

ios - 使用 UIViewPropertyAnimator 通过触摸中断动画

ios - 如何访问 iOS 中的设置

ios - 具有自动维度的 UITableView 无法正确显示单元格。总是该单元格中的最后一个标签有时会被破坏

ios - 更改 UITableView 中每个单元格的 UIButton

ios - 在圆形路径中对线形 CAShapeLayer 进行动画处理

ios - 运行 Non-Storyboard 应用程序的运行时错误

ios - 无法填充第二个 UITableView Controller

arrays - 使用额外的单词连接字符串数组中的所有元素

php - Alamofire 使用 multipart/form-data 上传图像

ios - 恢复为默认变量值?