0

サーバーからいくつかの文字列を解析し、それらの文字列をグループ化されたテーブルのヘッダーに配置しています。ビューに複数のグループ化されたテーブルがあります。

グループ化されたテーブルビューの私の通常の構造は

Header:
String1
string2
String3 

TableviewCells
.
.
.

1 つまたは 2 つの文字列が null を返すことがあります。つまり、行は空ですが、身長を次のように宣言しているので

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

ヘッダーの高さを動的に調整できない

文字列の 1 つが空の場合、ヘッダーの全体の高さを減らすにはどうすればよいですか? だから、むしろこれ

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;
}
4

2 に答える 2

3

ここでこれが機能するはずです。戻り値の高さを必要なものに変更できます。定数である必要はありません

// 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;
    }
}
于 2012-09-25T15:58:24.390 に答える
0

H2CO3が言ったように、NSArray(myArray)に文字列があり、セクションの対応するヘッダーにそれぞれを表示すると仮定すると、次のようなものでそれを行うことができます:

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

お役に立てれば。

于 2012-09-24T19:59:49.963 に答える