【问题标题】:Section header height differs when set manually手动设置时节标题高度不同
【发布时间】:2019-05-31 07:20:09
【问题描述】:

我正在尝试更改表格视图中特定节标题的高度。我使用下面的代码进行了测试,只是为了了解tableView(_:heightForHeaderInSection:) 的工作原理。

override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    print("DEBUG : ", section, tableView.sectionHeaderHeight)
    return tableView.sectionHeaderHeight
}

下面的调试打印显示该方法被多次调用,但标题高度始终为 18.0(这是默认值)。

DEBUG :  4 18.0
DEBUG :  4 18.0
DEBUG :  0 18.0
DEBUG :  0 18.0
DEBUG :  1 18.0
DEBUG :  1 18.0
DEBUG :  2 18.0
...
DEBUG :  3 18.0

现在,一旦我使用 18.0 作为返回的固定值(用于测试目的),表格视图的垂直扩展在视觉上被压缩,id est 部分更靠近,因此整个 UI 看起来不同。似乎各部分之间的空间减少了,因为第一部分的标题(垂直)只有一半可见。

override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    print("DEBUG : ", section, tableView.sectionHeaderHeight)
    return 18.0
}

这怎么可能?
可能是一个错误?

--- 更新 --- (13.06.2019)

我的问题是基于隐藏包含标题的部分 (2) 的意图。我意识到 tableView.sectionHeaderHeight 是错误的属性。我应该使用super.tableView(tableView, heightForHeaderInSection: section)

下面的代码可以按需要工作:

override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    if (section == 2) && myCondition {
        return CGFloat.leastNonzeroMagnitude
    }
    return super.tableView(tableView, heightForHeaderInSection: section)
}

尽管如此,我不知道18.0(见上文)来自哪里,因为我不使用 .xib 文件。

顺便说一句,super.tableView(tableView, heightForHeaderInSection: section) 总是返回 -1。我相信这会让 iOS 自行决定选择哪个高度。手动设置18.0(用于测试)使标题缩小,因为 iOS 选择的标题高度的自动值更高。

我没有找到打印此值的属性(必须在 30.0 附近 - 一个疯狂的猜测,仅此而已)。

【问题讨论】:

  • 可以显示viewForHeader吗?
  • 请也使用estimatedHeight方法,我认为它可以解决您的问题
  • @Alastar - 你的意思是截图吗?
  • 不,您的 viewForHeader 方法,如果您为每个标题创建了任何自定义视图。
  • @YogeshPatel - 添加estimatedHeightForHeaderInSection(始终为0.0)保持表格视图压缩,但完全删除第一部分的标题!其他部分的标题保留。这根本没有意义!

标签: ios swift uitableview uitableviewsectionheader


【解决方案1】:

根据您的代码,

override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return tableView.sectionHeaderHeight
}

您将sectionHeaderHeight 作为header height 返回,这实际上意味着UITableViewAutomaticDimension

open var sectionHeaderHeight: CGFloat // 默认为 UITableViewAutomaticDimension

根据苹果,

仅当委托未实现时才使用此非负值 tableView:heightForHeaderInSection: 方法。

使用print("DEBUG : ", section, tableView.sectionHeaderHeight) 时获得的值,即18.0.xibcustom headerViewheight

您仍然可以得到正确的 UI,因为 iOS 会在内部自动计算 header height runtime

这就是当您手动将18.0 作为header height 传递时,您的header 缩小的原因。

如果您想为每个section 使用单独的header height,则需要在tableView(_:heightForHeaderInSection:) 方法中传递manually,即

override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    switch section {
    case 0:
        return 50.0
    case 1:
        return 30.0
    default:
        return tableView.sectionHeaderHeight
    }
}

您可以根据您的要求在上述switch statement 中添加更多cases。如果您对此仍有任何困惑,请告诉我。

【讨论】:

  • 我没有 .xib 文件,因为一切都是通过情节提要完成的。因此,我仍然不知道18.0 的来源。我最初的意图是完全隐藏特定部分的标题。我现在通过上面的代码设法做到了。我编辑了我的首字母。邮政。请看上面。您自定义部分的代码工作正常。非常感谢。但是,iOS 为标题高度选择的默认值是多少?
  • .xib 我的意思是说 xcode 中的任何 UI。故事板也在其中。
猜你喜欢
  • 2011-05-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-07
  • 2016-08-05
相关资源
最近更新 更多