【问题标题】:Conditionally hide a section header of a grouped tableview with each section having title有条件地隐藏分组表视图的部分标题,每个部分都有标题
【发布时间】:2022-01-13 02:07:28
【问题描述】:

我有一个条件,我需要隐藏我的静态 tableView 的标题。我的 tableview 的样式是 grouped 并且每个部分 在部分 Header 中都有一个标题

我将第 5 部分的高度设置为 CGFloat.leastNormalMagnitude,因为我希望它消失但它不起作用。

override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        if hideFifthRow{
             if section == 4 {
                return CGFloat.leastNormalMagnitude
            }
        }
        return tableView.sectionHeaderHeight
}

谁能帮忙。

【问题讨论】:

    标签: ios hide uitableviewsectionheader


    【解决方案1】:
    override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    if hideFifthRow{
    if section == 4 {
    return 0.1
    }
    }
    return tableView.sectionHeaderHeight
    }
    

    【讨论】:

    • 不,这不起作用,因为标题标题字段不为空。
    • 好的。我认为标题的标题为 nil,这就是您试图隐藏该部分的标题的原因。
    【解决方案2】:

    所以,我找到了标题无法隐藏的原因。如果标题标题字段中有任何内容,则将部分高度设置为可能的最小高度将不起作用。

    所以,在

    override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
            if hideFifthRow{
                 if section == 4 {
                    return CGFloat.leastNormalMagnitude
                }
            }
            return tableView.sectionHeaderHeight
    }
    

    这是我需要额外执行的操作-

    override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        if hideFifthRow{
             if section == 4 {
                return nil
            }
        }
        return sectionTitles[section]
    }
    

    注意,我必须将标题标题存储在一个数组中,并通过titleForHeaderInSection 委托方法以编程方式设置标题。对于我的隐藏子句,我必须先返回 nil 才能摆脱标题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-02
      • 2017-07-16
      • 1970-01-01
      • 1970-01-01
      • 2016-05-19
      • 1970-01-01
      • 2018-12-26
      • 2012-04-02
      相关资源
      最近更新 更多