jsetting32
jsetting32

Reputation: 1632

Hide TableViewHeaderView for a specific TableView section

So I have this Tableview with a few sections, (3) to be exact. I want there to be headers for sections 2 and 3, not for the first sections..

Heres what i've done:

- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    NSString *sectionName;

    UIView *tempView;
    tempView = [[UIView alloc]initWithFrame:CGRectMake(0,0,300,20)];
    tempView.backgroundColor=[UIColor grayColor];

    UILabel *tempLabel = [[UILabel alloc]initWithFrame:CGRectMake(10,0,300,20)];
    tempLabel.backgroundColor = [UIColor clearColor];
    tempLabel.shadowColor = [UIColor blackColor];
    tempLabel.shadowOffset = CGSizeMake(0,2);
    tempLabel.textColor = [UIColor whiteColor]; //here you can change the text color of header.
    tempLabel.font = [UIFont fontWithName:@"Helvetica" size:14.0f];


    switch (section)
    {
            break;
        case 1:
        {
            sectionName = NSLocalizedString(@"Information", @"Information");
            tempLabel.text = sectionName;
            [tempView addSubview:tempLabel];
        }
            break;

        case 2:
        {
            sectionName = NSLocalizedString(@"Tools", @"Tools");
            tempLabel.text = sectionName;
            [tempView addSubview:tempLabel];
        }
            break;

    }
    return tempView;
}

I am confused on what needs to be done... Heres a pic of whats going on :TableViewHeader

Upvotes: 0

Views: 106

Answers (2)

jsetting32
jsetting32

Reputation: 1632

So while I was writing this question I cam to a conclusion... but maybe its not the more efficient?

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    CGFloat headerheight = 20.0;

    switch (section)
    {
        case 0: { headerheight = 0.0; } break;
        case 1: { headerheight = 20.0; } break;
        case 2: { headerheight = 20.0; } break;
    }
    return headerheight;
}

If anyone has any suggestions on how I don't need to implement this tableview delegate method, please speak up. I feel like I just wouldnt need to return a view for the specified section rather than say a sections header is 0. But I'm not completely sure at the moment, the problem is SOLVED but maybe not solved correctly?

Heres a pic of the result of the solution.

Solution Pic

Upvotes: 0

Mike Mertsock
Mike Mertsock

Reputation: 12015

In the case of section == 0, you are still setting tempView to a new instance of UIView and returning that value, you just aren't setting the title of the label. Also as you learned you should return 0 for tableView:heightForHeaderInSection: for section 0.

Upvotes: 2

Related Questions