arachide arachide - 2 months ago 7
iOS Question

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

Answer

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;
}