UITableViewController中的第二个tableview

时间:2015-11-05 12:39:22

标签: ios iphone swift uitableview

在我的故事板中,我有UITableViewController静态单元格和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中添加第二个表格视图

2 个答案:

答案 0 :(得分:1)

过了一天,我找到了解决这个问题的方法。灵感来自这些帖子:

因此,我没有在静态单元格中放置Container View,而是在单元格内放置embedded(在我的情况下,第4节的第一行),UITableViewController为另一个UIViewController或{{1}}取决于您的需求。相应的tableview代码转到另一个新文件。设置新表的方法与往常一样。

答案 1 :(得分:0)

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

祝你好运,Aj

相关问题