如何使用tableview制作粘性页脚和页眉?

时间:2016-09-17 20:54:51

标签: ios swift storyboard

我在故事板中有一个UITableView,还有一个标题和页脚,如图所示。

screenshot http://i63.tinypic.com/293j7ll.png

然而,当我移动单元格时标题消失(看起来标题是顶部单元格)。页脚也一样。在我浏览了所有细胞后,它出现了。 我试图将标题和页脚移到Cell层次结构之外。但这导致不再可见。

如何使页眉和页脚粘性?

2 个答案:

答案 0 :(得分:3)

不要将章节页眉和页脚与表格页眉和页脚混淆。

  • 当您滚动时,未分组表格中的节页眉和页脚会固定到表格的顶部和底部。

  • 但正如你所说的那样,表格页眉和页脚有点像单元格:它们位于第一部分之前和最后一部分之后,它们随表格一起滚动。

如果那不是您想要的 - 也就是说,如果您想要在表格上方和表格下方显示某些内容,那么您需要将它们作为完全独立的视图放在表格视图的上方和下方:

thing above
table
thing below

当然,如果你这样做,就不能使用UITableViewController。您必须拥有一个普通的视图控制器,将表视图作为嵌入式视图,将表视图控制器作为视图控制器。

答案 1 :(得分:0)

使用静态UITableView时,无法将自定义视图添加为标题。您需要像这样设置页眉和页脚:

//Header
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    //return your custom view here
}

//Footer
override func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
    //return your custom view here
}

您还必须指定页眉/页脚的高度:

override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    //return the height
}

override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
    //return the height
}