3

我不知道为什么我不能让我的部分标题高度根据文本量进行调整。

我在这方面找到的大多数信息都提供了有关如何根据文本量调整单元格行高的以下步骤:

  1. 将标签添加到情节提要中的 tableView 单元格,并将自动约束设置为标签。
  2. 在属性检查器中将行数设置为 0。
  3. 将以下代码添加到 ViewDidLoad 中的 swift 文件中:

    tableView.estimatedRowHeight = 50.0
    tableView.rowHeight = UITableViewAutomaticDimension
    

这在制作具有动态高度的单元格行时非常适合我,但我无法让它与节标题一起使用。

我认为这就像添加一样简单:

tableView.sectionHeaderHeight = UITableViewAutomaticDimension;
tableView.estimatedSectionHeaderHeight = 50.0;

但这不起作用。我知道指定行高的代码将取消自动布局约束,所以我认为问题可能出在我拥有的某些代码中,它使部分在点击手势时展开/折叠,并且它有一个图像(指向向上的箭头/向下基于该部分是展开还是折叠)。这就是我认为可能导致问题的原因:

func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
    //recast your view as a UITableViewHeaderFooterView
    let header: UITableViewHeaderFooterView = view as! UITableViewHeaderFooterView
    header.contentView.backgroundColor = UIColor.colorWithHexString(hexStr: "#4f9c25")
    header.textLabel?.textColor = UIColor.white


    if let viewWithTag = self.view.viewWithTag(kHeaderSectionTag + section) {
        viewWithTag.removeFromSuperview()
    }

    let borderTop = UIView(frame: CGRect(x:0, y:0, width: tableView.bounds.size.width, height: 1.0))
    borderTop.backgroundColor = UIColor.self.init(red: 5/255, green: 16/255, blue: 28/255, alpha: 1.0)
    header.addSubview(borderTop)


    let headerFrame = self.view.frame.size
    let theImageView = UIImageView(frame: CGRect(x: headerFrame.width - 32, y: 13, width: 18, height: 18));
    theImageView.image = UIImage(named: "Chevron-Dn-Wht")
    theImageView.tag = kHeaderSectionTag + section
    header.addSubview(theImageView)


    // make headers touchable
    header.tag = section
    let headerTapGesture = UITapGestureRecognizer()
    headerTapGesture.addTarget(self, action: #selector(WeedsControlledVC.sectionHeaderWasTouched(_:)))
    header.addGestureRecognizer(headerTapGesture)
}

如果这不是问题,我也尝试了以下线程中的建议,但我也无法让它工作。

动态设置 tableHeaderView 高度

任何想法我做错了什么?

4

0 回答 0