ios 以编程方式移除 UITableView 的头部并自动调整内容大小以填充移除区域

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5164291/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 19:05:11  来源:igfitidea点击:

Programmatically remove the header of UITableView and automatically resizes the content to fill in the removed area

iosiphoneuitableviewinterface-builder

提问by code007

I have added a UIButtonin the header section of the UITableViewvia the interface builder and would like to remove the access to the button in certain cases. I have tried to use the .hidden = TRUEproperty but it just hides the button from the view, leaving a white space behind. How do I programmatically remove the header of UITableViewand have the table's content automatically resizes to fill in the removed header area?

UIButtonUITableView通过界面构建​​器的标题部分添加了一个,并希望在某些情况下删除对按钮的访问。我试图使用该.hidden = TRUE属性,但它只是从视图中隐藏了按钮,留下了一个空白区域。如何以编程方式删除UITableView表头并自动调整表格内容的大小以填充删除的表头区域?

回答by Bogatyr

If you want to remove the table's header view, just set the myTable.tableHeaderView property to nil. If what you have is actually a section header, then you need to return nil from the viewForHeaderInSection method and call [myTableView reloadData]

如果要删除表的标题视图,只需将 myTable.tableHeaderView 属性设置为 nil。如果您拥有的实际上是节标题,那么您需要从 viewForHeaderInSection 方法返回 nil 并调用 [myTableView reloadData]

回答by mts

You could also do:

你也可以这样做:

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.0;
}

This seems to work for my (single) section header and avoids the need for reloadData.

这似乎适用于我的(单个)节标题,并避免了reloadData.

Note that:

注意:

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section

is still called, but its return value seems to be ignored.

仍然被调用,但它的返回值似乎被忽略了。

回答by Patrick Ridd

I created a boolean property called removeHeaderand then when ever I want to remove my header I call:

我创建了一个布尔属性removeHeader,然后当我想删除我的标题时调用:

func removeSectionHeader() {
        removeHeader = true
        self.tableView.reloadData()
    }

Then when my tableView reloads it will call this tableView delegate method:

然后当我的 tableView 重新加载时,它会调用这个 tableView 委托方法:

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        if removeHeader {
            return 0.0
        } else {
            let height = self.tableView.sectionHeaderHeight
            return height
        }
    }