Ios UITableView标题与其单元格内容重叠
我正在开发一个UITableViewController视图,该视图在UITableView标题中有一堆高度不同的内容。整个页眉的高度要求会有所不同,因此我需要根据其中显示的内容动态设置它 下面是我在寻找潜在解决方案后一直在尝试的内容。UITableView.automaticDimension似乎没有任何作用。将tableView.sectionHeaderHeight设置为静态数字是可行的,但我无法动态设置它 这一切都是通过编程实现的 我这样做对吗?基本上,在所有子视图都添加到headerView之后,我想设置整个标题的高度 ViewDidLoad:Ios UITableView标题与其单元格内容重叠,ios,swift,uitableview,Ios,Swift,Uitableview,我正在开发一个UITableViewController视图,该视图在UITableView标题中有一堆高度不同的内容。整个页眉的高度要求会有所不同,因此我需要根据其中显示的内容动态设置它 下面是我在寻找潜在解决方案后一直在尝试的内容。UITableView.automaticDimension似乎没有任何作用。将tableView.sectionHeaderHeight设置为静态数字是可行的,但我无法动态设置它 这一切都是通过编程实现的 我这样做对吗?基本上,在所有子视图都添加到headerV
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, viewForHeaderInSection section: Int) -> UIView? {
let v = headerView
v.translatesAutoresizingMaskIntoConstraints = false
v.addSubview(headingLabel)
v.addSubview(subheadingLabel)
v.addSubview(additionalContent)
headerConstraints()
return v
}
这就是我所看到的:
这就是我希望看到的:
有什么建议吗
谢谢 如果将tableView的clipsToBounds设置为true,我相信这可以解决 使用代码:
tableView.clipsToBounds = true
使用界面生成器:
我相信,如果您将tableView的clipsToBounds设置为true,则可以解决此问题 使用代码:
tableView.clipsToBounds = true
使用界面生成器:
将UITableView样式分组使用。将UITableView样式分组使用。问题是:
v.translatesAutoresizingMaskIntoConstraints = false
当您将自定义视图作为表视图部分的页眉/页脚提供时,不需要修改返回视图本身的自动调整大小掩码。据我所知,这样做会破坏表视图的内部设置
<>但是如果您登记了UITabeWebDeAdHealTeVIEW子类作为页眉/页脚,您就必须考虑这个选项,因为您正在控制配置整个页眉/页脚。
现在,我想你的
let v = headerView
. . .
headerConstraints()
代码是正确的。如果是这样的话,上述改变应该会像预期的那样起作用
否则,以下是您可以尝试的示例:
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let v = UIView()
let headingLabel = UILabel(frame: .zero)
headingLabel.translatesAutoresizingMaskIntoConstraints = false
headingLabel.text = "View Header"
headingLabel.font = UIFont.systemFont(ofSize: 30, weight: .heavy)
headingLabel.textColor = .black
let subheadingLabel = UILabel(frame: .zero)
subheadingLabel.translatesAutoresizingMaskIntoConstraints = false
subheadingLabel.text = "Subheader Content"
subheadingLabel.font = UIFont.systemFont(ofSize: 15, weight: .semibold)
subheadingLabel.textColor = .gray
let additionalLabel = UILabel(frame: .zero)
additionalLabel.translatesAutoresizingMaskIntoConstraints = false
additionalLabel.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."
additionalLabel.numberOfLines = 0
additionalLabel.font = UIFont.systemFont(ofSize: 15, weight: .regular)
additionalLabel.textColor = .white
let additionalContent = UIView(frame: .zero)
additionalContent.translatesAutoresizingMaskIntoConstraints = false
additionalContent.backgroundColor = .black
additionalContent.addSubview(additionalLabel)
additionalLabel.leadingAnchor.constraint(equalTo: additionalContent.leadingAnchor, constant: 10).isActive = true
additionalLabel.trailingAnchor.constraint(equalTo: additionalContent.trailingAnchor, constant: -10).isActive = true
additionalLabel.topAnchor.constraint(equalTo: additionalContent.topAnchor, constant: 10).isActive = true
additionalContent.bottomAnchor.constraint(equalTo: additionalLabel.bottomAnchor, constant: 10).isActive = true
let stackView = UIStackView(frame: .zero)
stackView.translatesAutoresizingMaskIntoConstraints = false
stackView.axis = .vertical
stackView.alignment = .fill
stackView.distribution = .fill
stackView.spacing = 8
stackView.addArrangedSubview(headingLabel)
stackView.addArrangedSubview(subheadingLabel)
stackView.addArrangedSubview(additionalContent)
v.addSubview(stackView)
stackView.leadingAnchor.constraint(equalTo: v.leadingAnchor, constant: 16).isActive = true
stackView.trailingAnchor.constraint(equalTo: v.trailingAnchor, constant: -16).isActive = true
stackView.topAnchor.constraint(equalTo: v.topAnchor, constant: 16).isActive = true
stackView.bottomAnchor.constraint(equalTo: v.bottomAnchor, constant: -16).isActive = true
return v
}
如果需要自动计算高度,请不要忘记:
override func viewDidLoad() {
super.viewDidLoad()
. . .
tableView.sectionHeaderHeight = UITableView.automaticDimension
tableView.estimatedSectionHeaderHeight = 100
. . .
}
问题是:
v.translatesAutoresizingMaskIntoConstraints = false
当您将自定义视图作为表视图部分的页眉/页脚提供时,不需要修改返回视图本身的自动调整大小掩码。据我所知,这样做会破坏表视图的内部设置
<>但是如果您登记了UITabeWebDeAdHealTeVIEW子类作为页眉/页脚,您就必须考虑这个选项,因为您正在控制配置整个页眉/页脚。
现在,我想你的
let v = headerView
. . .
headerConstraints()
代码是正确的。如果是这样的话,上述改变应该会像预期的那样起作用
否则,以下是您可以尝试的示例:
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let v = UIView()
let headingLabel = UILabel(frame: .zero)
headingLabel.translatesAutoresizingMaskIntoConstraints = false
headingLabel.text = "View Header"
headingLabel.font = UIFont.systemFont(ofSize: 30, weight: .heavy)
headingLabel.textColor = .black
let subheadingLabel = UILabel(frame: .zero)
subheadingLabel.translatesAutoresizingMaskIntoConstraints = false
subheadingLabel.text = "Subheader Content"
subheadingLabel.font = UIFont.systemFont(ofSize: 15, weight: .semibold)
subheadingLabel.textColor = .gray
let additionalLabel = UILabel(frame: .zero)
additionalLabel.translatesAutoresizingMaskIntoConstraints = false
additionalLabel.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."
additionalLabel.numberOfLines = 0
additionalLabel.font = UIFont.systemFont(ofSize: 15, weight: .regular)
additionalLabel.textColor = .white
let additionalContent = UIView(frame: .zero)
additionalContent.translatesAutoresizingMaskIntoConstraints = false
additionalContent.backgroundColor = .black
additionalContent.addSubview(additionalLabel)
additionalLabel.leadingAnchor.constraint(equalTo: additionalContent.leadingAnchor, constant: 10).isActive = true
additionalLabel.trailingAnchor.constraint(equalTo: additionalContent.trailingAnchor, constant: -10).isActive = true
additionalLabel.topAnchor.constraint(equalTo: additionalContent.topAnchor, constant: 10).isActive = true
additionalContent.bottomAnchor.constraint(equalTo: additionalLabel.bottomAnchor, constant: 10).isActive = true
let stackView = UIStackView(frame: .zero)
stackView.translatesAutoresizingMaskIntoConstraints = false
stackView.axis = .vertical
stackView.alignment = .fill
stackView.distribution = .fill
stackView.spacing = 8
stackView.addArrangedSubview(headingLabel)
stackView.addArrangedSubview(subheadingLabel)
stackView.addArrangedSubview(additionalContent)
v.addSubview(stackView)
stackView.leadingAnchor.constraint(equalTo: v.leadingAnchor, constant: 16).isActive = true
stackView.trailingAnchor.constraint(equalTo: v.trailingAnchor, constant: -16).isActive = true
stackView.topAnchor.constraint(equalTo: v.topAnchor, constant: 16).isActive = true
stackView.bottomAnchor.constraint(equalTo: v.bottomAnchor, constant: -16).isActive = true
return v
}
如果需要自动计算高度,请不要忘记:
override func viewDidLoad() {
super.viewDidLoad()
. . .
tableView.sectionHeaderHeight = UITableView.automaticDimension
tableView.estimatedSectionHeaderHeight = 100
. . .
}
谢谢你的建议。我在ViewDidLoad中添加了tableView.clipsToBounds=true,但它对输出没有影响。嗯,我可以知道您是如何设置带有副标题标签的tableView约束的吗?如果您输入了错误的高度,clipsToBounds将真的对其进行剪裁。谢谢您的建议。我在ViewDidLoad中添加了tableView.clipsToBounds=true,但它对输出没有影响。嗯,我可以知道你是如何使用副标题标签设置tableView约束的吗?如果你传递了错误的高度,clipsToBounds将真的剪裁它。当你已经准备好headerView时,为什么要使用自动高度?你应该通过准确的高度。若表格样式是普通的,向下滚动一点后,标题视图将显示在单元格上。检查您是否面临这种行为,如果是这种情况,我建议您使用不透明的标题视图,而不是透明的。当headerView内容更新时,您可能需要重新加载表或节。当您已经准备好headerView时,为什么要使用自动高度?你应该通过准确的高度。若表格样式是普通的,向下滚动一点后,标题视图将显示在单元格上。检查您是否面临这种行为,如果是这种情况,我建议您使用不透明的标题视图,而不是透明的。您可能需要重新加载表或节,当headerView的内容更新时首选。谢谢!您的解决方案和部分示例解决了我的问题。谢谢!你的解决方案和你的部分例子解决了我的问题。这有助于解决我遇到的另一个问题。谢谢这帮助我解决了另一个问题。谢谢