2012-09-24 32 views
0

我從服務器解析一些字符串,並將這些字符串放在分組表的表頭中,我在視圖上有多個分組表。IOS動態設置分組表頭的標頭高度

我對gropued的tableview規則的結構是

Header: 
String1 
string2 
String3 

TableviewCells 
. 
. 
. 

有時一個字符串的兩個返回null。因此,這意味着行是空的,但是自從我宣佈我的身高一樣

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

我不能安排頭的高度dyncamically

我怎樣才能減少頭部的總高度,如果其中一個字符串爲空? 因此而不是這個

String1 
String2 


Tableview 

這應該發生:

String1 
String2 

TableViewCells 

我所問究竟是

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

我的代碼是:

-(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; 
} 
+1

EUH的高度,你不* *需要從該委託方法返回一個常數... – 2012-09-24 18:46:38

+0

是的,但不是'heightForHeaderInSection'在'viewForHeaderInSection'之前被調用?也許他需要使用singleton來設置值,然後在'heightForHeaderInSection'中調用它? –

+0

@Mord你是如何設置或獲取這些字符串的getter方法? –

回答

2

這裏這應該工作,你可以更改返回值,任何你想要的,他們不必須是常量

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

像H2CO3說假設你有一個NSArray(myarray的)的字符串和您展示各一節的其對應的頭,你可以做到這一點的東西,如:

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

希望這有助於。