是否可以使用自动布局获取动态表格视图节标题高度?

Jor*_*n H 102 uitableview ios autolayout ios8

在iOS 8中,您可以通过简单地设置估计的行高来获得100%动态表格视图单元格,然后使用"自动布局"在单元格中布局元素.如果内容物的高度增加,则细胞的高度也会增加.这非常有用,我想知道是否可以在表格视图中为节标题完成相同的专长?

例如,可以创建一个UIViewin tableView:viewForHeaderInSection:,添加一个UILabel子视图,为视图指定标签的自动布局约束,并使视图的高度增加以适应标签的内容,而不必实现tableView:heightForHeaderInSection:

viewForHeaderInSection状态文档:"只有在实现tableView:heightForHeaderInSection:时,此方法才能正常工作." 我还没有听说过iOS 8有什么变化.

如果一个人不能这样做,模仿这种行为的最佳方法是什么?

Jor*_*n H 253

这个有可能.它与iOS 8中实现的动态单元高度一起是新的.

这很简单.只需添加viewDidLoad:

self.tableView.sectionHeaderHeight = UITableViewAutomaticDimension;
self.tableView.estimatedSectionHeaderHeight = 25;
Run Code Online (Sandbox Code Playgroud)

然后覆盖viewForHeaderInSection并使用"自动布局"来约束视图中的元素.而已!无需实施heightForHeaderInSection.实际上sectionHeaderHeight也不需要说明,我只是为了清楚而添加它.

请注意,在iOS 11中,单元格和页眉/页脚视图默认使用估计的高度.唯一要做的就是提供一个估计的高度,以便更好地告知系统预期的结果.默认值是,UITableViewAutomaticDimension但您应该提供更好的估计值,如果可能的话,它们将是它们的平均高度.

  • 这不适合我.我的节标题能够自动计算高度,但它与节中的单元格重叠.我正在使用xib文件设置标题视图并在界面构建器中设置我的约束.那是我的问题吗?你们在哪里创造约束?谢谢! (26认同)
  • 这里的另一个关键点是,您的表格单元格还需要配置为自动高度(UITableViewAutomaticDimension)计算,以便在部分上工作. (7认同)
  • 你必须有`estimatedSectionHeaderHeight`或`tableView:estimatedHeightForHeaderInSection:`AND`sectionHeaderHeight`或`tableView:heightForHeaderInSection:`.此外,如果您正在使用委托方法,则必须为估计返回大于1.00的值(完全未记录的行为),否则将不会调用高度委托,并将使用默认的表视图标题高度. (5认同)
  • 正如这里所承诺的那样是示例代码:https://github.com/ebetabox/DynamicCellSectionHeight (4认同)
  • 如果它有效,这很酷,但是文档没有提及它,如果我没记错的话,WWDC会话也没有."UITableViewAutomaticDimension"的文档说"如果你在`tableView:heightForHeaderInSection:`或`tableView:heightForFooterInSection:`中返回这个常量,``UITableView`使用的高度适合从`tableView:titleForHeaderInSection:`或`tableView返回的值: titleForFooterInSection:`(如果标题不是'nil`)." (3认同)
  • @Joey我也使用相同的动态标题代码,但它在IOS 10的iOS9中不起作用它完美的工作你对此有任何想法 (3认同)
  • 我终于搞定了!关键因素:我正在从xib加载标题视图.不要在IB中使用UITableViewCell作为基础,使用普通的UIView并在那里设置约束.它按预期工作,根据标签文本长度等动态增长,稍后将上传示例代码. (2认同)
  • 看来,如果使用`estimatedSectionHeaderHeight`,你也必须使用`estimatedHeight`。这一单一更改修复了重叠在我的单元格上的部分标题。 (2认同)

Cla*_*lis 25

这可以通过设置(或返回)estimatedSectionHeaderHeight表视图来完成.

如果您的部分标题在设置后与单元格重叠,请estimatedSectionHeaderHeight确保您也使用了一个estimatedRowHeight.

(我正在添加这个答案,因为第二段包含了一个问题的答案,可以在阅读所有可能遗漏的评论后找到.)


Ahs*_*him 8

陷入同一问题中,标头的高度一直为零,除非我为委托提供了固定的高度heighForHeaderInSection

尝试了很多解决方案,其中包括

self.tableView.sectionHeaderHeight = UITableViewAutomaticDimension;
self.tableView.estimatedSectionHeaderHeight = 73`
Run Code Online (Sandbox Code Playgroud)

但是没有任何效果。我的手机也在使用正确的自动版式。行通过使用以下代码来动态更改其高度,但节头却没有。

self.tableView.estimatedRowHeight = 135
self.tableView.rowHeight = UITableViewAutomaticDimension`
Run Code Online (Sandbox Code Playgroud)

解决方法是非常简单和太怪异,但我不得不执行的委托方法,而不是1个代码,estimatedSectionHeaderHeightsectionHeaderHeight肚里如下我的情况。

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

func tableView(_ tableView: UITableView, estimatedHeightForHeaderInSection section: Int) -> CGFloat {
    return 73
}
Run Code Online (Sandbox Code Playgroud)


小智 6

我试过了

self.tableView.sectionHeaderHeight = UITableViewAutomaticDimension;
self.tableView.estimatedSectionHeaderHeight = 25;
Run Code Online (Sandbox Code Playgroud)

但它没有使用多行标签正确标题.添加这个来解决我的问题:

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    // Recalculates height
    tableView.beginUpdates()
    tableView.endUpdates()
}
Run Code Online (Sandbox Code Playgroud)


Mr.*_*ani 6

迅捷4+

工作(经过100%测试)

如果同时需要根据内容具有动态高度的部分和行,则可以使用以下代码:

在viewDidLoad()上编写以下行:

    self.globalTableView.estimatedRowHeight = 20
    self.globalTableView.rowHeight = UITableView.automaticDimension

    self.globalTableView.sectionHeaderHeight =  UITableView.automaticDimension
    self.globalTableView.estimatedSectionHeaderHeight = 25;
Run Code Online (Sandbox Code Playgroud)

现在,我们使用UITableView Delegate方法设置行高和节高:

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
   {
       return UITableView.automaticDimension
   }
   func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {

       return UITableView.automaticDimension

   }
Run Code Online (Sandbox Code Playgroud)


Cod*_*dus 5

就我而言:

  1. 以编程方式设置不起作用

self.tableView.sectionHeaderHeight = UITableViewAutomaticDimension.

  1. 设置在情节提要中不起作用

  2. 覆盖heightForHeaderInSection 工作

override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return UITableViewAutomaticDimension
}
Run Code Online (Sandbox Code Playgroud)

测试环境:

  • Mac 操作系统 10.13.4
  • XCode 版本 9.4.1
  • 模拟器 iPhone 8 Plus