out*_*orm 15 uitableview ios uitableviewautomaticdimension uitableviewsectionheader
我遇到了自动/动态UITableView节头视图的问题,其中包含一个包装的UILabel(numberOfLines = 0).高度未正确计算,尤其是在滚动表格并重新使用视图后.有时UILabel包裹,有时它被截断,有时一个或多个标签不可见,有时标签之间有额外的间距.自定义视图包含一个带有三个UILabel的垂直UIStackView,其中一个包装.
可以在https://github.com/outerstorm/tableviewHeaderTest找到演示该问题的完整示例应用程序.
截面标题高度在viewDidLoad中设置为自动,具有以下内容:
tableView.sectionHeaderHeight = UITableViewAutomaticDimension
tableView.estimatedSectionHeaderHeight = 30.0
Run Code Online (Sandbox Code Playgroud)
并且还实现了以下heightForHeaderInSection,试图让它工作:
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return UITableViewAutomaticDimension
}
Run Code Online (Sandbox Code Playgroud)
我也尝试在各个点调用setNeedsLayout()和layoutIfNeeded()无济于事.任何建议将不胜感激.
以下是应用中看到的行为的屏幕截图.第一部分是截止的,第二部分太高了:
小智 1
一般来说
heightForHeaderInSection
Run Code Online (Sandbox Code Playgroud)
之前总是打电话
viewForHeaderInSection
Run Code Online (Sandbox Code Playgroud)
当使用 UITableViewAutomaticDimension 时,sectionheader 高度只会计算一次,除非手动调用 tableview.reloadData() 。
在代码中,您每次都会更改节标题文本。高度是第一次计算的,不会自动改变。
您可以将设置功能更改为:
func setup(someNum: Int) {
let text = String(repeating: "This is where the really long text goes so that it will wrap lines appropriately", count: someNum)
mainLabel.text = text
}
Run Code Online (Sandbox Code Playgroud)
并将节索引传递给函数
| 归档时间: |
|
| 查看次数: |
5308 次 |
| 最近记录: |