【问题标题】:All the section titles showing in one section在一个部分中显示的所有部分标题
【发布时间】:2017-02-08 23:10:53
【问题描述】:

我有一个分组的表格视图,出于某种原因,所有部分标题仅显示在一个部分中。所以结果是标题在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
}

谢谢!

【问题讨论】:

    标签: ios swift uitableview autolayout uitableviewsectionheader


    【解决方案1】:

    更新:

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

    在您的 HeaderView 实现中:

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

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

    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
    }
    

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

    【讨论】:

    • 嗨@Mischa,感谢您的建议。 numberOfSections 已实现,但问题仍然存在。
    • 你是对的。还有其他问题。更新我的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-25
    • 2016-05-19
    • 1970-01-01
    • 1970-01-01
    • 2021-08-29
    • 2015-04-22
    • 1970-01-01
    相关资源
    最近更新 更多