我正在使用UITableViewController
视图,该视图在UITableView
标头中具有许多不同高度的内容。整个标题的高度要求会有所不同,因此我需要根据其中显示的内容动态设置它。
以下是我在寻找潜在解决方案后一直在尝试的方法。 UITableView.automaticDimension
似乎没有任何作用。将tableView.sectionHeaderHeight
设置为静态数字是可行的,但我不能完全将其动态设置。
全部通过编程方式完成。
我要采用正确的方法吗?基本上在所有子视图都添加到headerView之后,我要设置整个标题的高度。
ViewDidLoad:
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self as! UITableViewDataSource
tableView.sectionHeaderHeight = UITableView.automaticDimension
tableView.estimatedSectionHeaderHeight = 200;
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
}
应该设置标题高度吗?
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return UITableView.automaticDimension
}
override func tableView(_ tableView: UITableView, estimatedHeightForHeaderInSection section: Int) -> CGFloat {
return 200
}
添加标题内容:
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let v = headerView
v.translatesAutoresizingMaskIntoConstraints = false
v.addSubview(headingLabel)
v.addSubview(subheadingLabel)
v.addSubview(additionalContent)
headerConstraints()
return v
}
有什么建议吗?
谢谢!