一个部分中显示的所有部分标题

All the section titles showing in one section

我有一个分组表视图,但出于某种原因,所有部分标题仅显示在一个部分中。所以结果是标题在 section = 0 中全部重叠。所有其他部分都是空白的。

我试过调试,发现当 section == 1 时,headerLabel 为 nil。

我正在使用自定义视图作为 header,代码如下:

class HeaderView: UIView {

let headerLabel =  UILabel()

override init(frame: CGRect) {
    super.init(frame: frame)
    self.translatesAutoresizingMaskIntoConstraints = false
    addCustomView()
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

func addCustomView() {
    self.addSubview(headerLabel)
    NSLayoutConstraint.activate(headerLabelConstraints())
}

private func headerLabelConstraints() -> [NSLayoutConstraint] {
    let leadingAnchor = headerLabel.leadingAnchor.constraint(equalTo: self.layoutMarginsGuide.leadingAnchor)
    let trailingAnchor = headerLabel.trailingAnchor.constraint(equalTo: self.layoutMarginsGuide.trailingAnchor)
    let topAnchor = headerLabel.topAnchor.constraint(equalTo: self.layoutMarginsGuide.topAnchor)
    let heightAnchor = headerLabel.heightAnchor.constraint(equalToConstant: 44)
    headerLabel.translatesAutoresizingMaskIntoConstraints = false
    return [leadingAnchor, trailingAnchor, topAnchor, heightAnchor]
}

}

下面是我在 tableView 中的调用方式:

override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
    let header = view as! HeaderView
}

//for reference: sectionHeaderHeight = 44
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return tableView.sectionHeaderHeight
}

//for reference: sectionTitles = ["title1", "title2", "title3"]
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let headerView = HeaderView(frame: CGRect.zero)
    headerView.layoutMargins = UIEdgeInsets(top: 0, left: 20, bottom: 0, right: 20)
    headerView.headerLabel.text = sectionTitles[section]
    headerView.headerLabel.textColor = UIColor.black
    return headerView
}

谢谢!

更新:

我注意到其他几个问题。解决方法如下:

在您的 HeaderView 实施中:

  1. init(frame:) 方法中删除 self.translatesAutoresizingMaskIntoConstraints = false

(A UITableView 始终需要为单元格和 header / 页脚视图设置固定的行高。如果您使用自动布局来确定行高,系统首先计算必要的高度,然后为相应的视图设置相应的边界,然后添加一个固定宽度和一个固定高度约束 "simulate" 这些边界。如果你在视图上停用 translatesAutoresizingMaskIntoConstraints 你也会停用最后一个步骤和视图永远不会得到正确的高度设置。)

  1. 添加底部约束:

    let bottomAnchor = headerLabel.bottomAnchor.constraint(equalTo: self.layoutMarginsGuide.bottomAnchor)
    // ...
    return [leadingAnchor, trailingAnchor, topAnchor, bottomAnchor, heightAnchor]
    

(如果你只将标签的顶部、左侧和右侧布局锚与其父视图的布局锚连接起来,那么父视图的高度不会绑定到标签的高度。它的高度实际上将为 0。这这就是为什么你所有的标签都堆叠在一起的原因。)

在包含对 table 视图的引用的视图控制器实现中:

  1. 将以下行添加到其 viewDidLoad() 方法中:

    tableView.sectionHeaderHeight = UITableViewAutomaticDimension
    tableView.estimatedSectionHeaderHeight = 44
    

(这就是您告诉 table 视图您不想使用固定的 header 视图高度但希望 table 视图通过解决您添加到视图的自动布局约束来自动计算高度。估计的高度不需要正好是 44。它应该接近您的 header 视图高度的平均值。)


原答案:

您似乎没有 "told" 您的 table 查看它应该有多少个部分。只需实施

func numberOfSections(in tableView: UITableView) -> Int {
    return sectionTitles.count
}

在您的 table 视图的数据源中。 (如果省略,部分数默认为 1。)


顺便说一句: 您的实施

override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
    let header = view as! HeaderView
}

确实没有意义。它只是简单地创建一个常量,并不对该常量做任何事情。编译器实际上应该为这一行显示一个黄色警告。因此,除非您需要在显示 header 视图之前执行某些操作,否则只需删除整个方法即可。