ViewForHeaderInSection - What to return without a title?

I have UIViewControllerwith a few built in it UITableView. For some tables, I need to show my own header view with several labels. For other tables, I don’t want to show the title at all (i.e. I want the first cell in to myTable2be at the top of the frame for myTable2). Here is roughly what I have:

- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (tableView == self.myTable1)
    // Wants Custom Header
    {        
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0,0,tableView.contentSize.width,20);

        // ... Do stuff to customize view ...

        return view;
    }
    elseIf (tableView == self.myTable2)
    // Wants No header
    {
        return nil;
        // also tried
        // return [[UIView alloc] initWithFrame:CGRectMake(0,0,0,0)];
        // but that didn't work either
    }
}

This works great for custom headers, but for tables where I don't need a heading, it shows a white box. I figured it return nil;would prevent any header from being displayed for this table. Is this assumption correct? Is there anything else that rewrites this? How can I make sure that nothing is shown?

+3
3

, viewForHeaderInSection, heightForHeaderInSection. 0 .

+8

, .

tableView.sectionHeaderHeight = UITableViewAutomaticDimension
tableView.estimatedSectionHeaderHeight = 50

, .

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        if section == 2 {
            return 0
        }
        return UITableViewAutomaticDimension
    }
+1

check this code, according to @rmaddy's comments

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

if (tableView == self.myTable1)
    // Wants Custom Header

{
    //custome header height
    return 20.0f;//height of custom header

    }
    elseIf (tableView == self.myTable2)
                    // Wants No header
    {
        return 0;
    }


}
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
if (tableView == self.myTable1)
    // Wants Custom Header
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0,0,tableView.contentSize.width,20);

                    // ... Do stuff to customize view ...

                    return view;
                    }
                    elseIf (tableView == self.myTable2)
                    // Wants No header
                    {
                        return nil;
                    }
}
0
source

All Articles