10

I used the code below to set the height of header view of UITableView

- (CGFloat)tableView:(UITableView *)tableView
heightForHeaderInSection:(NSInteger)section
{
    CGFloat height = 0.0; //  

    return height;
}

but it display abnormal, shown as below

enter image description here

there is a white block, your comment welcome

1
  • please include datasource methods used Commented Mar 13, 2013 at 15:33

5 Answers 5

29

The Tableview frame is not set properly.check in code or nib

The delegate return the headerview height and it is proper in the Table since there is no space between the edge of tableview and cell

use setFrame method to properly set via code

Setting height to 0 will not change section height of grouped table.So as a tweak use a very smaller value but not 0 like

- (CGFloat)tableView:(UITableView *)tableView
heightForHeaderInSection:(NSInteger)section
{
    CGFloat height = 0.001;   
    return height;
}
Sign up to request clarification or add additional context in comments.

2 Comments

If you want to get rid of that header view that shows up this does it. This allows for re-use of a grouped as a plain UITableView without that header view popping up at the top. -- Thx @Lithu T.V.
It works but need to override heightForFooterInSection method also!
10

swift 4

As other answers said, if you return 0 for height, it is means you say set default.

use small number like CGFloat.leastNonzeroMagnitude to solve problem.

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return CGFloat.leastNonzeroMagnitude
    }

hope this help.

Comments

7

You should return nil from your implementation of - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

//return header view for specified section of table view
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    //check header height is valid
    if([self tableView:tableView heightForHeaderInSection:section] == 0.0)
    {
        //bail
        return nil;
    }

    //create header view
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, self.view.frame.size.width, [self tableView:tableView heightForHeaderInSection:section])];

    //

    //return header view
    return view;
}

Comments

3

0 means default. Make the height extremely small such as 0.01 to "hide" it.

1 Comment

Apple has a lot of such obscure hacks. Another example which is more known is when you set a UILabel's numberOfLines to 0. You'd normally expect that 0 means zero lines, yet it means flexible number of lines!
0

In my case, setting header height to 0.001 didn’t work. Still, there was some space showing. So, I had to do this-

tableView.sectionHeaderTopPadding = 0.0

Since iOS 15, UITableView contains a new property called sectionHeaderTopPadding which specifies the amount of padding above each section header.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.