【问题标题】:Second tableview in UITableViewControllerUITableViewController 中的第二个表视图
【发布时间】:2015-11-05 12:39:22
【问题描述】:

在我的故事板中,我有一个UITableViewController,其中包含 static 单元格和 4 个部分。

  • 第 1 部分有 6 行,第 1 行高度比其他部分高得多。
  • 第 2 节有 1 行。
  • 第 3 节有 4 行。
  • 第 4 节,1 行。

在第 4 节的行中,我添加到带有 动态 单元格的 UITableView (table2)(无标题,仅 1 节)。

所以我关注了一些帖子,并在下面有一些代码:

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    if tableView == table2 {
        return 1
    }
    return super.numberOfSectionsInTableView(tableView)
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if tableView == table2 {
        return 4
    }
    return super.tableView(tableView, numberOfRowsInSection: section)
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    if tableView == table2 {
        let cell = tableView.dequeueReusableCellWithIdentifier(textCellIdentifier, forIndexPath: indexPath) as! CustomerCellView
        cell.setCustomCell()
        return cell
    }
    return super.tableView(tableView, cellForRowAtIndexPath: indexPath)
}

运行它时,它会成功构建。默认表正确显示,但 table2 没有。 table2 似乎遵循默认表的格式,所以问题是

  • table2 也显示了和默认表的第一个表头相同的表头,即使我没有设置。
  • 如果我将行数设置为大于 6,则应用在模拟器中崩溃。
  • 我把断点放在tableView == table2,编译器确实进入了if语句
  • table2 委托和数据源连接到 UITableViewController 本身。

如果有人知道我的代码有什么问题,或者有人知道如何在UITableViewController 中添加第二个表格视图

【问题讨论】:

    标签: ios iphone swift uitableview


    【解决方案1】:

    一天后,我想出了解决这个问题的方法。受到这些帖子的启发:

    因此,我没有在静态单元格中放置 UITableView,而是在单元格内放置了 Container View(在我的情况下,第 4 节的第一行),其中 embedded 到另一个 UITableViewController 或 @ 987654326@ 取决于您的需要。相应的 tableview 代码转到另一个新文件。并且设置新表的方式和往常一样。

    【讨论】:

      【解决方案2】:

      您必须创建一个 uiviewcontroler 并在该视图中添加 2 个表视图。然后,您可以将它们链接到具有数据源和委托的视图控制器。然后在视图控制器类中添加委托。为表格视图添加正常功能后。您可以使用重用标识符来分类需要哪个。

      祝你好运,阿杰

      【讨论】:

      • 感谢您的回复。我确实尝试过,但是当我在UIViewController 中设置static cells 时,出现红色错误消息:“静态表视图仅在嵌入UITableViewController 实例时有效”
      • 你做了数据源和代理出口吗?
      • 并在视图控制器类中添加委托?
      • 是的,我做到了。并且在设置为静态单元格后立即出现红色错误消息
      • 尝试动态单元格,但不确定。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-05
      • 2015-04-13
      相关资源
      最近更新 更多