Mord Fustang
Mord Fustang

Reputation: 1553

IOS dynamically setting header height of grouped TableView

I am parsing some strings from server and putting those strings in header of grouped tables, I have multiple grouped tables on a view.

my regular structure for gropued tableview is

Header:
String1
string2
String3 

TableviewCells
.
.
.

Sometimes one or two of the strings returns null. So that means line is empty but since I declare my height like

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

I cannot arrange height of the headers dyncamically

How can I reduce the total height of the header if one of the strings is empty? So rather then this

String1
String2


Tableview

This should happen:

String1
String2

TableViewCells

What I am exactly asking is

if [string3 length]==0 
    set heightForHeaderInSection: 60
else
    set heightForHeaderInSection: 90

My code is:

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


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection: (NSInteger)section {
      NSString * presenter = [[self agenda] getBriefingPresenter:section];
      NSString * time = [[self agenda] getBriefingTime:section];
     NSString * subject = [[[[self agenda] getMeetingBriefings] objectAtIndex:section] objectForKey:@"subject"];

    UILabel *subjectLabel = [[UILabel alloc] initWithFrame:CGRectMake(45, 0, 484, 23)];
    subjectLabel.textColor = [UIColor colorWithRed:0/256.0 green:84/256.0 blue:129/256.0 alpha:1.0];
    subjectLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:25];
    subjectLabel.text = subject;
    subjectLabel.backgroundColor = [UIColor clearColor];
    [subjectLabel sizeToFit];



    UILabel *timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(45, subjectLabel.frame.size.height, 484, 23)];
    timeLabel.textColor = [UIColor colorWithRed:51/256.0 green:51/256.0 blue:51/256.0 alpha:1.0];
    timeLabel.font = [UIFont fontWithName:@"Century Gothic" size:21];
    timeLabel.text = time;
    timeLabel.backgroundColor = [UIColor clearColor];
    [timeLabel sizeToFit];

    // Create label with section title
    UILabel *presenterLabel = [[UILabel alloc] initWithFrame:CGRectMake(45, timeLabel.frame.origin.y + timeLabel.frame.size.height, 484, 23)];
    presenterLabel.textColor = [UIColor colorWithRed:71/256.0 green:71/256.0 blue:71/256.0 alpha:1.0];
    presenterLabel.font = [UIFont fontWithName:@"Century Gothic" size:18];
    presenterLabel.text = presenter;
    presenterLabel.backgroundColor = [UIColor clearColor];
    [presenterLabel sizeToFit];

    // Create header view and add label as a subview
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(100, 300, 320, 400)];


    [view addSubview:subjectLabel];
    [view addSubview:timeLabel];
    [view addSubview:presenterLabel];


    return view;
}

Upvotes: 0

Views: 5096

Answers (2)

SpaceDust__
SpaceDust__

Reputation: 4914

Here this should work, you can change the height at return value to whatever you want they dont have to be constants

// set header height of gropued tableview
-(CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section {

    NSString * presenter = [[self agenda] getBriefingPresenter:section];
    NSString * time = [[self agenda] getBriefingTime:section];
    NSString * subject = [[[[self agenda] getMeetingBriefings] objectAtIndex:section] objectForKey:@"subject"];

    //possible incoming data scenes
    if ([time length]==0 || [presenter length]==0 || [subject length]==0) {
        return 60;
    }
    else if(([time length]==0 && [presenter length]==0 ) || ([time length]==0 && [subject length]==0 ) || ([presenter length]==0 && [subject length]==0 )){
        return 45;
    }
    else if ([time length]==0 && [presenter length]==0 && [subject length]==0){
        return 30;
    }
    else{
        return 90;
    }
}

Upvotes: 3

CSolanaM
CSolanaM

Reputation: 3368

Like H2CO3 said and assuming you have the strings in an NSArray (myArray) and you show each one in its corresponding header of section, you can do that with something like:

-(CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section {
    if ([[myArray objectAtIndex:[section intValue]] length] == 0) {
        return 60;
    } else {
        return 90;
    }
}

Hope this helps.

Upvotes: 0

Related Questions