简体   繁体   English

有条件地删除分组表视图的 header 部分

[英]Conditionally remove a section header of a grouped tableview

I have a condition based on which, I need to hide the header for my static tableView.我有一个条件,我需要为我的 static tableView 隐藏 header。 My tableview's style is grouped and every section has a title in the section Header.我的 tableview 的样式是grouped的,每个部分在 Header 部分都有一个标题。

I am setting the height of the 5th section to CGFloat.leastNormalMagnitude because I want it to disappear but it is not working.我将第 5 部分的高度设置为CGFloat.leastNormalMagnitude因为我希望它消失但它不起作用。

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

Can anyone please help.任何人都可以请帮忙。

So, I figured out the reason for header not being able to hide.所以,我找到了 header 无法隐藏的原因。 If there is anything in the header title field, making the the being to be the smallest possible height will not work.如果 header 标题字段中有任何内容,则将其设为可能的最小高度将不起作用。

So, on top of所以,在上面

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

Here is what I need to perform additionally-这是我需要额外执行的操作-

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

Note, I had to store the header titles in an array and set the titles programmatically from titleForHeaderInSection delegate method too.注意,我必须将 header 标题存储在一个数组中,并通过titleForHeaderInSection委托方法以编程方式设置标题。 For my hiding clause, I had to return nil to get rid of the title first.对于我的隐藏条款,我必须先返回 nil 才能摆脱标题。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM