Pyae Phyoe Shein
Pyae Phyoe Shein

Reputation: 13827

How to hide header if string/object is null in UITableView

I want to hide header and its space if if string/object is null in UITableView. Here is my coding. In this code, if object is null, header space remains there, but it shouldn't.

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

    CompanyObj *compObj=[self.arrCompList objectAtIndex:section];

    NSString *compname;
    if ([compObj.compName isEqualToString:@""]) {
        [self NochangeHeight];
        return nil;
    } else {
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 36)];
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, tableView.frame.size.width, 36)];
        if ([compObj.clientList count] != 0) {
            compname = compObj.compName;

            [label setFont:[UIFont fontWithName:@"AvenirNextLTPro-Regular" size:25.0f]];
            label.textColor = [UIColor blackColor];
            label.textAlignment = NSTextAlignmentLeft;
            [label setText:compname];
            label.backgroundColor = [UIColor orangeColor];
            [view setBackgroundColor:[UIColor orangeColor]];

            [view addSubview:label];
            [self changeHeight];
        }
        return view;
    }
}

- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return headerHeight;
}

- (void)changeHeight {
    [_clientListGroupTableView beginUpdates];
    headerHeight = 50.0;
    [_clientListGroupTableView endUpdates];
}

- (void)NochangeHeight {
    [_clientListGroupTableView beginUpdates];
    headerHeight = 0.0;
    [_clientListGroupTableView endUpdates];
}

Upvotes: 0

Views: 61

Answers (2)

Kutay Demireren
Kutay Demireren

Reputation: 650

You can make the height 0 if it is "", rather than controlling in viewForHeaderInSection.

The method viewForHeaderInSection arrange the view of header according to what you are returning. But it does arrange on to the existing header. So header is always there, and you are just making labels, texts etc. onto that header. As a consequence, when you say it to be "nil", since it is just what should be on the header, programme will understand it as "There is nothing to put on to the header". Thus, header will just be an empty space.

In order not to see header, you should change the height, within the method heightForHeaderInSection. So if you implement also that method as:

- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    CompanyObj *compObj=[self.arrCompList objectAtIndex:section];
    if ([compObj.compName isEqualToString:@""]) {
        return 0;
    } else {
        return headerHeight;
    }
}

Upvotes: 1

ZeMoon
ZeMoon

Reputation: 20274

You have to return a different value in the heightForHeaderInSection: method instead.

- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    CompanyObj *compObj=[self.arrCompList objectAtIndex:section];
    if ([compObj.compName isEqualToString:@""]) {
        return 0;
    } else {
        return headerHeight;
    }
}

Upvotes: 1

Related Questions