heightForHeaderInSection只调用一次

时间:2016-10-06 19:05:35

标签: ios uitableview layout header swift3

Swift中的编码3.拥有一个带有自定义单元格和标题的tableView。

我有一个带有自定义单元格和标题的tableView。标题中包含两(2)个标签,并且由于标签可能很长,因此具有动态单元格高度。我的问题是第一次配置tableView和部分时,标签显示为应该,但是,在向下滚动然后备份标题后,它应该出现。布局不知何故打破了。

When tableView first loads and user scrolls down everything appears fine

如下所示,在我向下滚动然后返回到单元格后,标签将被截止。

After scrolling down and back up label is cut off

在打印出调用的方法后,我发现第一次向下滚动tableView时会调用以下两(2)个覆盖函数。

override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    print("\(section) heightForHeaderInSection")
    print("\(section) returning AUTO for header")
    return UITableViewAutomaticDimension
}

override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    print("\(section) viewForHeaderInSection")
    let header = tableView.dequeueReusableCell(withIdentifier: "QuestionHeader") as! QuestionHeader
    header.delegate = self
    header.contentView.backgroundColor = UIColor.groupTableViewBackground
    header.questionTextLabel.text = String(questionStringArray[section])
    header.questionNumberLabel.text = (String(section + 1) + ")")
    return header.contentView
}

但是当我向后滚动时,只调用了viewForHeader函数,我认为因为高度不再被设置为UITableViewAutomaticDimension,标签会被截断?

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

您应该从header方法返回header.contentView而不是tableView: viewForHeaderInSection:

override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
  let header = tableView.dequeueReusableCell(...
  ...
  return header
}
相关问题