0

我正在从服务器解析一些字符串并将这些字符串放在分组表的标题中,我在一个视图上有多个分组表。

我的 gropued tableview 的常规结构是

Header:
String1
string2
String3 

TableviewCells
.
.
.

有时,其中一两个字符串返回 null。所以这意味着行是空的,但因为我声明我的高度像

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

我无法动态安排标题的高度

如果其中一个字符串为空,如何减少标题的总高度? 所以而不是这个

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 回答