繁体   English   中英

UITableViewController节的页眉和页脚未显示

[英]UITableViewController section header and footer no showing

Swift 3,XCode 8.3动态原型单元格。 该视图(类CustomTableViewController: UITableViewController )位于一个单独的情节提要中,该情节提要从另一个情节提要中引用,并带有一个表的序列,该表本身已嵌入到Nav Controller中。

实现了以下方法:

override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat
override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat
override func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView?
override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int)

如果我在navcontroller嵌入视图-控制, viewForFooterInSection将调用和查看是可见的。 willDisplayHeaderView仍然没有被调用。 编辑 :这不是嵌入它正在将我的CustomTableViewController设置为初始视图控制器。 当Nav Controller为IVC时,则调用viewForFooterInSection,而当CustomTableViewController(嵌入Nav Controller)为IVC时,则不调用viewForFooterInSection。

如果我将表格更改为静态单元格, willDisplayHeaderView调用viewForFooterInSection并将调用viewForFooterInSection ,但仅当将视图嵌入到Nav Controller中时,才会再次调用。

我试图避免将其嵌入到另一个导航控制器中,但是无论如何都不会调用willDisplayHeaderView

您忘记了这些代表:

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

override func tableView(_ tableView: UITableView, estimatedHeightForFooterInSection section: Int) -> CGFloat {
    return 100
}

标头也一样。 编码愉快!

为添加代理方法

  1. 标头的高度(返回标头的适当高度)
  2. 查看标题
  3. 页脚高度(返回适当的页脚高度)
  4. 查看页脚

如果在Viewcontroller中使用表,请检查将Delegate和数据源分配给表。

您可以实现表视图的以下方法

extension DemoViewController: UITableViewDelegate {

//setting header height to 100
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return 100
}

//setting footer height to 100
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
    return 100
}

//setting header view
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let headerView = UIView(frame: CGRect(x:0, y:0, width: tableView.frame.size.width, height: 50))
    headerView.backgroundColor = UIColor.red
    return headerView
}

//setting footer view
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
    let footerView = UIView(frame: CGRect(x:0, y:0, width: tableView.frame.size.width, height: 50))
    footerView.backgroundColor = UIColor.green
    return footerView
}

func tableView(_ tableView: UITableView, willDisplayFooterView view: UIView, forSection section: Int) {
    //customization for footer goes here
}

func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
    //customization for header goes here
}
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM