iOS 8中的新功能,您只需设置估计的行高,即可获得100%动态表格视图单元格,然后使用“自动布局”在单元格中布局元素。如果内容物的高度增加,则细胞的高度也会增加。这非常有用,我想知道是否可以在表格视图中为节标题完成相同的专长?
例如,可以在UIView
中创建tableView:viewForHeaderInSection:
,添加UILabel
子视图,针对视图指定标签的自动布局约束,并使视图的高度增加以适应标签的内容,而不必实现tableView:heightForHeaderInSection:
?
viewForHeaderInSection
状态的文档:“只有在实现tableView:heightForHeaderInSection:时,此方法才能正常工作。”我还没有听说过iOS 8有什么变化。
如果不能这样做,模仿这种行为的最佳方式是什么?
答案 0 :(得分:239)
这是可能的。它与iOS 8中实现的动态单元格高度一起是新的。
这很简单。只需在viewDidLoad
:
self.tableView.sectionHeaderHeight = UITableViewAutomaticDimension;
self.tableView.estimatedSectionHeaderHeight = 25;
然后覆盖viewForHeaderInSection
并使用“自动布局”来约束视图中的元素。而已!无需实施heightForHeaderInSection
。实际上sectionHeaderHeight
也不需要说明,我只是为了清楚而添加它。
请注意,在iOS 11中,单元格和页眉/页脚视图默认使用估计的高度。唯一要做的就是提供一个估计的高度,以便更好地告知系统预期的结果。默认值为UITableViewAutomaticDimension
,但您应提供更好的估算值,即可能的平均高度。
答案 1 :(得分:22)
这可以通过在表格视图上设置(或返回)procedure TForm34.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
CanClose:= DoneProcessing;
if not(DoneProcessing) then begin
ShowMessage('hold on');
end;
end;
来完成。
如果您的部分标题在设置estimatedSectionHeaderHeight
后与您的单元格重叠,请确保您也使用estimatedSectionHeaderHeight
。
(我添加了这个答案,因为第二段包含了一个问题的答案,在阅读了一些可能遗漏的评论之后可以找到这个问题。)
答案 2 :(得分:6)
我试过
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// Recalculates height
tableView.beginUpdates()
tableView.endUpdates()
}
但它没有使用多行标签正确标题。 添加了这个来解决我的问题:
def hasNoX_2(s):
if isinstance(s, str):
return 'x' not in s.lower()
return False
print(hasNoX_2('Xenon'))
print(hasNoX_2('eenon'))
答案 3 :(得分:4)
在同一问题中,标头的高度一直为零,直到我为 protected override async void OnStart()
{
bool WasWorkSuccess=await IsSomeThingWorkingAsync();
//do something with the boolean
}
的委托中提供固定的高度。
尝试了很多解决方案,其中包括
heighForHeaderInSection
但是没有任何效果。我的手机也在使用正确的自动版式。通过使用以下代码,行可以动态更改其高度,但节头却没有。
self.tableView.sectionHeaderHeight = UITableViewAutomaticDimension;
self.tableView.estimatedSectionHeaderHeight = 73`
此修复程序非常简单且怪异,但是我必须实现委托方法,而不是self.tableView.estimatedRowHeight = 135
self.tableView.rowHeight = UITableViewAutomaticDimension`
和estimatedSectionHeaderHeight
的1行代码,这对于我的情况如下。
sectionHeaderHeight
答案 4 :(得分:1)
就我而言:
self.tableView.sectionHeaderHeight = UITableViewAutomaticDimension
。
在情节提要中设置的不起作用。
覆盖heightForHeaderInSection
有效。
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { return UITableViewAutomaticDimension }
测试环境:
答案 5 :(得分:0)
是的,它对我有用。我必须进行如下更改:
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let label = UILabel()
label.numberOfLines = 0
label.text = my own text
return label
}
答案 6 :(得分:0)
快速4 +
工作(经过100%测试)
如果您同时需要两个部分以及根据内容具有动态高度的行,则可以使用以下代码:
在viewDidLoad()上写以下行:
self.globalTableView.estimatedRowHeight = 20
self.globalTableView.rowHeight = UITableView.automaticDimension
self.globalTableView.sectionHeaderHeight = UITableView.automaticDimension
self.globalTableView.estimatedSectionHeaderHeight = 25;
现在,我们已使用UITableView Delegate方法设置行高和节高:
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
{
return UITableView.automaticDimension
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return UITableView.automaticDimension
}
答案 7 :(得分:0)
Swift 5,iOS 12+
它对我有用的唯一方法是
tableView.sectionHeaderHeight = UITableView.automaticDimension
tableView.estimatedSectionHeaderHeight = 40
tableView.estimatedRowHeight = 80
然后为自定义标题视图元素设置约束,以便自动布局引擎可以确定它们的 x、y 位置和行高。即
NSLayoutConstraint.activate([
titleLabel.leadingAnchor.constraint(equalTo: contentView.layoutMarginsGuide.leadingAnchor),
titleLabel.topAnchor.constraint(equalTo: contentView.topAnchor),
titleLabel.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -24)
])
答案 8 :(得分:-1)
我修改了iuriimoz answer。刚刚替换了viewWillAppear方法:
tableView.sectionHeaderHeight = UITableViewAutomaticDimension
tableView.estimatedSectionHeaderHeight = 25
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// Recalculates height
tableView.layoutIfNeeded()
}
还将tableView.layoutIfNeeded()添加到
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
tableView.layoutIfNeeded()
}
适用于iOS 10
tableView.beginUpdates()
tableView.endUpdates()
对我来说,在viewWillAppear上有“淡化”动画效果