gpt4 book ai didi

ios - Tableview 标题与单元格重叠

转载 作者:行者123 更新时间:2023-11-29 05:48:42 25 4
gpt4 key购买 nike

我有一个带有 TableView 的 View Controller 。 Tableview 有一个 TableView 标题和动态单元格。我添加了一个 UIView 作为 TableView 标题,它有多个具有动态高度的标签。一切正常。

但是当 View 的大小(用作 TableView 标题)由于 View 中使用的标签的额外高度(因为它由于额外的内容而扩展)而扩展时,就会出现问题。

因此,表格 View 的上部单元格被标题 View 隐藏或重叠。有人能帮我解决这个问题吗?所有这些都是使用 Storyboard完成的。

最佳答案

将 TableView 标题添加到 Storyboard 中时,无法更改它的高度。以编程方式创建 TableView header ,并在 heightForHeaderInSection 方法中返回 UITableView.automaticDimension

override func viewDidLoad() {
super.viewDidLoad()
tableView.estimatedSectionHeaderHeight = 100
tableView.sectionHeaderHeight = UITableView.automaticDimension
}

override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let header = UIView()

let lbl = UILabel()
lbl.text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum"
lbl.numberOfLines = 0
lbl.lineBreakMode = .byWordWrapping
lbl.translatesAutoresizingMaskIntoConstraints = false
header.addSubview(lbl)

header.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-(10)-[lbl(>=30)]-(10)-|", options: [], metrics: nil, views: ["lbl":lbl]))
header.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-(30)-[lbl]-(30)-|", options: [], metrics: nil, views: ["lbl":lbl]))
return header
}
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return UITableView.automaticDimension
}

关于ios - Tableview 标题与单元格重叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55881123/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com