Reputation: 1347
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.
Upvotes: 28
Views: 34375
Reputation: 4765
If you created your view in storyboard, you can hide it temporary with
tableView.tableHeaderView?.frame = CGRect.zero
To display it again, use
tableView.tableHeaderView?.frame = CGRect(x: 0, y: 0, width: tableView.frame.width, height: 50)
Upvotes: 6
Reputation: 882
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.
Objective 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+:
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
}
Upvotes: 1
Reputation: 51374
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). HOWEVER you must click on that header view, and drag the referencing outlet to the table view controller, and link it to "tableHeaderView" ... that part is not "magic".
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.
Upvotes: 83
Reputation: 619
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.
Upvotes: 14
Reputation: 3405
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
Upvotes: 0
Reputation:
//hidden sectionFooter
- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
return nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection: (NSInteger)section {
return 0.0;
}
Upvotes: 4
Reputation: 31
removeFromSuperview or set nil not work when "reloadData"
self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 0.0f, 0.001f)];
[self.tableView reloadData];
Upvotes: 3
Reputation: 4770
[self.tableView.tableHeaderView removeFromSuperview];
self.tableView.tableHeaderView = nil;
[self.tableView reloadData];
Upvotes: 4