-1

我的目标是在运行时更改 UITableView 部分标题的高度。让我更具体一点。默认高度为 44,但在触摸顶部之前滚动,高度将为 64。我创建了一个子类,UITableViewHeaderFooterView它使用自动布局。

我试过了

var frame = sectionHeader.frame
frame.size.height = 64
sectionHeader.frame = frame

并且

tableView.sectionHeaderHeight = 64

但没有什么对我有用。任何人都可以对这个问题有所了解。

4

3 回答 3

1
  1. 直接使用自动布局更改框架时不起作用
  2. 要更改 tableview 部分标题,您需要实现委托方法并在更改后重新加载数据

可选 func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat

于 2016-07-31T16:29:28.893 回答
1

你不需要子类化页脚来改变我的高度。

  1. 检查您仅将我的高度设置为:(无委托方法)

tableView.sectionHeaderHeight = 64

  1. 之后检查你在 tableView 上调用或“reloadData()”。或者

[UIView setAnimationsEnabled:NO];

[tableView 开始更新];

[tableView endUpdates];

[UIView setAnimationsEnabled:YES];

仅更改高度而不重新加载 tableView。

于 2016-07-31T16:36:39.310 回答
0

我认为您必须重新加载表格视图的整个部分,因为没有仅用于更新部分标题的公共 API。

self.isOnTop = true // or false
tableView.reloadSections(NSIndexSet(index: 0), withRowAnimation: .None)

并更改从适当的委托方法修改返回值的属性:

func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    isOnTop ? 44 : 64
}

您还可以尝试以下方法,无需重新加载即可更新布局:

tableView.beginUpdates()
tableView.endUpdates()

或移至支持 tableview 行为的集合视图解决方案:https ://github.com/jamztang/CSStickyHeaderFlowLayout

于 2016-07-31T16:42:37.190 回答