Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

set height to 0 for header view of UITableView

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

like image 591
arachide Avatar asked Mar 13 '13 15:03

arachide


3 Answers

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.

like image 126
moraei Avatar answered Sep 23 '22 04:09

moraei


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;
}
like image 25
Lithu T.V Avatar answered Sep 26 '22 04:09

Lithu T.V


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;
}
like image 33
Zack Brown Avatar answered Sep 23 '22 04:09

Zack Brown