【问题标题】:How to refresh a dynamic TableView Footer (Not Section Footer) using autolayout如何使用自动布局刷新动态 TableView 页脚(不是部分页脚)
【发布时间】:2017-10-11 12:14:26
【问题描述】:

我有一个带有两个标签的 TableView footerView,可以容纳多行。 我在 viewDidLayoutSubviews 中使用此代码:

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    label1.preferredMaxLayoutWidth = tableView.frame.size.width - Constant.footerMarginSpace
    label2.preferredMaxLayoutWidth = tableView.frame.size.width - Constant.footerMarginSpace
    footerView.setNeedsLayout()
    footerView.layoutIfNeeded()
    footerView.frame.size.height = footerView.systemLayoutSizeFitting(UILayoutFittingCompressedSize).height
    tableView.tableFooterView =  footerView
}

当标签值在视图显示之前设置时,这工作正常(标签最终会分布在多行并且视图的大小正在调整),但如果我在之后更新它们的内容,视图将不会更新其布局,即使使用 tableView.reloadData()。

如果我将另一个视图推到我的视图之上,然后将其关闭,则布局将是正确的。

更新 TableView 页脚布局和大小的正确方法是什么?

【问题讨论】:

  • 编辑后试试tableView.tableFooterView.layoutIfNeeded()。

标签: ios swift uitableview autolayout tableview


【解决方案1】:

我认为根据您的数据计算您需要多少行并使用返回正确的高度可能会更容易 tableView(_:heightForFooterInSection:)

reloadData被调用时,这个方法会被调用来布局你的tableview。

【讨论】:

  • 对不起,我看错了。所以如果你想去heightForFooterInSection,你可以使用最后一节的页脚。如果您想使用表格页脚,TableView 页眉和页脚的行为类似于单元格。您应该能够像使用普通 Cell 一样设置正确的 LayoutConstraints。然后页脚应该像任何其他自动调整大小的 tableview 单元格一样。像 Cell 一样,页脚应该有自己的布局。
  • 但最后一节页脚的行为不同,它很粘
猜你喜欢
  • 2016-10-04
  • 1970-01-01
  • 2015-08-15
  • 2015-05-26
  • 1970-01-01
  • 2019-10-07
  • 2011-10-12
  • 1970-01-01
  • 2019-04-20
相关资源
最近更新 更多