ios 我们如何隐藏 tableHeaderView 和 tableFooterView?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8079820/
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
How can we hide the tableHeaderView and tableFooterView?
提问by ICoder
I have two buttons which i add it in one in table-footer and other one in table-header,i know how to hide the headerview of the table using this codetable.tableHeaderView.hidden = YES;
but the problem is there is still space in the top portion of the table.That space is equal to the header-view size,but the view is hidden .It still has the space.How can we disable the table-header by removing this space.I hope you genius developers understand my question.Please help me.
Thanks in advance.
我有两个按钮,我在表页脚中添加一个按钮,在表头中添加另一个按钮,我知道如何使用此代码隐藏表的标题视图,table.tableHeaderView.hidden = YES;
但问题是表的顶部仍有空间.那个空间等于标题视图的大小,但是视图被隐藏了。它仍然有空间。我们如何通过删除这个空间来禁用表头。我希望你们天才开发人员理解我的问题。请帮助我。提前致谢。
回答by EmptyStack
Instead of hiding the header view you should do,
而不是隐藏你应该做的标题视图,
tableView.tableHeaderView = nil
And later if you want to show it then just assign it again,
稍后如果你想显示它,那么只需再次分配它,
tableView.tableHeaderView = tableHeaderView;
In Swift:
在斯威夫特:
class myTableViewController: UITableViewController {
@IBOutlet var tableHeaderView: UIView!
private func toggleHeaderView() {
if tableView.tableHeaderView == nil {
tableView.tableHeaderView = tableHeaderView
} else {
tableView.tableHeaderView = nil
}
}
}
on your Storyboard, simply drag a UIView in to the table view. It will "magically" become the table view header (if you do another one, it will become the table view footer). HOWEVERyou must click onthat header view, and dragthe referencing outlet to the table view controller, and link it to "tableHeaderView" ... that part is not "magic".
在 Storyboard 上,只需将 UIView 拖入表格视图即可。它将“神奇地”成为表格视图标题(如果你再做一个,它将成为表格视图页脚)。但是,您必须单击该标题视图,然后将引用出口拖到表视图控制器,并将其链接到“tableHeaderView”...该部分不是“魔术”。
Note that because of the "!" in the declaration, you have to remember to drag the link on Storyboard or you'll get a runtime error during testing, so that's a good thing.
请注意,因为“!” 在声明中,你必须记住在Storyboard上拖动链接,否则在测试过程中会出现运行时错误,所以这是一件好事。
回答by Alfa
Try:
尝试:
tableView.tableHeaderView?.removeFromSuperview()
tableView.tableHeaderView = UIView(frame: CGRect(x: 0, y: 0, width: 0, height: CGFloat.leastNonzeroMagnitude))
tableView.layoutIfNeeded()
Set an UIView with height as CGFloat.leastNonzeroMagnitude instead of '0'. This will remove the blank space appearing at the top after removing the tableViewHeader. This worked for me.
将高度设置为 CGFloat.leastNonzeroMagnitude 而不是“0”的 UIView。这将删除 tableViewHeader 后出现在顶部的空白区域。这对我有用。
回答by catanore
If you created your view in storyboard, you can hide it temporary with
如果您在故事板中创建了视图,则可以使用以下命令暂时隐藏它
tableView.tableHeaderView?.frame = CGRect.zero
tableView.tableHeaderView?.frame = CGRect.zero
To display it again, use
要再次显示它,请使用
tableView.tableHeaderView?.frame = CGRect(x: 0, y: 0, width: tableView.frame.width, height: 50)
tableView.tableHeaderView?.frame = CGRect(x: 0, y: 0, width: tableView.frame.width, height: 50)
回答by mac
[self.tableView.tableHeaderView removeFromSuperview];
self.tableView.tableHeaderView = nil;
[self.tableView reloadData];
回答by Lithium Lee
removeFromSuperview or set nil not work when "reloadData"
removeFromSuperview 或 set nil 在“reloadData”时不起作用
self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 0.0f, 0.001f)];
[self.tableView reloadData];
回答by MengXiang
//hidden sectionFooter
- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
return nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection: (NSInteger)section {
return 0.0;
}
回答by KeithTheBiped
Often a ghost header view will continue to appear after a header view has been set using a "viewForHeaderInSection" and is later programmatically hidden or set to nil. To make it go away completely explicitly set the sectionHeaderHeight = 0.
通常,在使用“viewForHeaderInSection”设置标题视图后,幽灵标题视图将继续出现,并且稍后以编程方式隐藏或设置为 nil。要使其完全消失,请明确设置 sectionHeaderHeight = 0。
Objective C:
目标 C:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger) section
{
if (self.viewMode == SectionHeaderShouldBeHidden)
{
tableView.sectionHeaderHeight = 0;
return nil;
}
else if (self.viewMode == SectionHeaderShouldAppear)
{
//section text as a label
UILabel *lbl = [[UILabel alloc] init];
lbl.textAlignment = NSTextAlignmentCenter;
lbl.font = [UIFont boldSystemFontOfSize:13];
lbl.textColor = [UIColor whiteColor];
[lbl setBackgroundColor:App.secondaryColor];
tableView.sectionHeaderHeight = 20;
if (self.queryResultsViewModel.items.count == 0)
return lbl;
lbl.text = @"Section Header Text";
return lbl;
}
}
Swift 4+:
斯威夫特 4+:
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
if !self.viewMode == HeaderShouldBeShown
{
self.listView.sectionHeaderHeight = 0
return nil
}
let lbl: UILabel = UILabel.init()
lbl.textAlignment = NSTextAlignment.center
lbl.font = UIFont.boldSystemFont(ofSize: 13)
lbl.textColor = UIColor.white
lbl.backgroundColor = UIColor.black
tableView.sectionHeaderHeight = 20
lbl.text = "Section Header Text"
return lbl
}
回答by Amal T S
if tableView.tableHeaderView == nil{
tableView.tableHeaderView = self.headerHolder
}
else
{
tableView.tableHeaderView = nil
}
}
You should create an outlet headerHolder & its must be a strong property. If we used a weak property for headerHolder, once it is nil, it is released & you cant set it again as header
您应该创建一个插座 headerHolder 并且它必须是一个强大的属性。如果我们为 headerHolder 使用了一个弱属性,一旦它为零,它就会被释放并且你不能再次将它设置为 header