0

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

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

我使用自定义视图作为标题,代码如下:

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
}

谢谢!

4

1 回答 1

0

更新:

我注意到了其他几个问题。以下是解决它们的方法:

在您的HeaderView实施中:

  1. self.translatesAutoresizingMaskIntoConstraints = falseinit(frame:)方法中删除。

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

  1. 添加底部约束:

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

(如果您仅将标签的顶部、左侧和右侧布局锚与其父视图的布局锚连接,则父视图的高度不会绑定到标签的高度。它的高度实际上是 0。这就是为什么所有标签都堆叠在顶部彼此。)

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

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

    tableView.sectionHeaderHeight = UITableViewAutomaticDimension
    tableView.estimatedSectionHeaderHeight = 44
    

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


原答案:

似乎您还没有“告诉”您的表格视图应该有多少部分。简单实现

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

在表视图的数据源中。(如果省略,节数默认为 1。)


顺便说一句:你的实现

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

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

于 2017-02-09T00:10:07.600 回答