无法更改正确的UITableViewCell高度根据ios中的标签文本

时间:2014-04-29 13:22:01

标签: ios uitableview

有时候我没有得到正确的UITableViewCell高度。我的表格单元格包含三个标签,如我在附带的屏幕截图中所提到的。请检查我的以下代码并帮助我解决此问题。

enter image description here

这是我的代码:

-(CGFloat)getTextHeightForIndex:(NSInteger)index
{
    CGSize maximumSize = CGSizeMake(242, 10000);

    Agenda *agenda = [[Agenda alloc] init];
    agenda = [arrayMyAgenda objectAtIndex:index];

    NSString *strTotalText = [NSString stringWithFormat:@"%@  %@  %@", agenda.program_name, agenda.program_hall, [NSString stringWithFormat:@"%@ - %@", agenda.pg_start_time, agenda.pg_end_time]];

    CGSize labelHeightSize = [strTotalText sizeWithFont: [UIFont fontWithName:@"Helvetica" size:16.0f]
                                     constrainedToSize:maximumSize
                                         lineBreakMode:NSLineBreakByWordWrapping];
    return labelHeightSize.height +20;
}

#pragma mark - UITableViewDelegate

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return  1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.arrayMyAgenda count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [self getTextHeightForIndex:indexPath.row];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    Agenda *myAgenda = [[Agenda alloc] init];
    myAgenda = [self.arrayMyAgenda objectAtIndex:indexPath.row];

    NSString *cellIdentifier = [NSString stringWithFormat:@"cell%d",indexPath.row];

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if(!cell) {

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];

        UILabel *lblTitleText   = nil;
        UILabel *lblAreaText  = nil;
        UILabel *lblTimingText   = nil;

        //First label for Title
        lblTitleText = [[UILabel alloc] initWithFrame:CGRectMake(10, 6, 305, 24)];
        lblTitleText.font = [UIFont fontWithName:@"Helvetica" size:16.0];
        lblTitleText.backgroundColor = [UIColor clearColor];
        lblTitleText.text = myAgenda.program_name;
        lblTitleText.textAlignment = NSTextAlignmentLeft;
        lblTitleText.numberOfLines = 0;
        lblTitleText.tag = 10;
        [lblTitleText sizeToFit];
        lblTitleText.textColor = [UIColor colorWithRed:(59/255.0) green:(59/255.0) blue:(59/255.0) alpha:1.0];
        [cell.contentView addSubview:lblTitleText];

        //Second label for Area name
        lblAreaText = [[UILabel alloc] initWithFrame:CGRectMake(10, lblTitleText.frame.size.height + 4, 305, 21)];
        lblAreaText.text = myAgenda.program_hall;
        lblAreaText.font = [UIFont fontWithName:@"Helvetica" size:16.0];
        lblAreaText.backgroundColor = [UIColor clearColor];
        lblAreaText.tag = 20;
        lblAreaText.textColor = [UIColor colorWithRed:(161/255.0) green:(163/255.0) blue:(160/255.0) alpha:1.0];
        lblAreaText.textAlignment = NSTextAlignmentLeft;
        lblAreaText.numberOfLines = 0;
        [lblAreaText sizeToFit];

        [cell.contentView addSubview:lblAreaText];

        //Third label for timings
        lblTimingText = [[UILabel alloc] initWithFrame:CGRectMake(10, lblTitleText.frame.size.height + lblAreaText.frame.size.height + 2, 305, 21)];
        lblTimingText.text = [NSString stringWithFormat:@"%@ - %@", myAgenda.pg_start_time, myAgenda.pg_end_time];
        lblTimingText.font = [UIFont fontWithName:@"Helvetica" size:16.0];
        lblTimingText.backgroundColor = [UIColor clearColor];
        lblTimingText.tag = 30;
        lblTimingText.textColor = [UIColor colorWithRed:(161/255.0) green:(163/255.0) blue:(160/255.0) alpha:1.0];
        [cell.contentView addSubview:lblTimingText];

        return cell;
    }
    else
    {
        ((UILabel *)[cell.contentView viewWithTag:10]).text = myAgenda.program_name;
        ((UILabel *)[cell.contentView viewWithTag:20]).text = myAgenda.program_hall;
        ((UILabel *)[cell.contentView viewWithTag:30]).text = [NSString stringWithFormat:@"%@ - %@", myAgenda.pg_start_time, myAgenda.pg_end_time];

        UIView *selectionColor = [[UIView alloc] init];
        selectionColor.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"middleRowSelected.png"]];
        cell.selectedBackgroundView = selectionColor;

        cell.backgroundView = [[UIView alloc] initWithFrame:CGRectZero];
        cell.backgroundView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"recentchatcell.jpg"]];

        return cell;
    }
}

提前致谢。

2 个答案:

答案 0 :(得分:1)

-(CGFloat)getTextHeightForIndex:(NSInteger)index中尝试此操作:而不是CGSize使用CGRect。为我工作。

CGRect labelHeightSize = [strTotalText boundingRectWithSize:CGSizeMake(280.f, CGFLOAT_MAX) options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading) attributes:@{NSFontAttributeName:[UIFont fontWithName:@"Helvetica" size:16.0f]} context:nil];
return ceilf(labelHeightSize.size.height) + 50.0f;

答案 1 :(得分:0)

我认为问题是你得到的组合字符串的高度将显示为三个不同的标签。你应该得到每个标签的高度,然后添加它们以获得单元格高度。从显示在它们上面的字符串中获取每个标签的高度并将它们加在一起以获得单元格高度是不是更有意义?

试试这个:

-(CGFloat)getTextHeightForIndex:(NSInteger)index
{
CGSize maximumSize = CGSizeMake(305, 10000);

Agenda *agenda = [[Agenda alloc] init];
agenda = [arrayMyAgenda objectAtIndex:index];

CGFloat cellHeight = 0;

CGSize titleLabelHeightSize = [agenda.program_name sizeWithFont: [UIFont fontWithName:@"Helvetica" size:16.0f]
                                  constrainedToSize:maximumSize
                                      lineBreakMode:NSLineBreakByWordWrapping];
cellHeight += titleLabelHeightSize.height > 24 ? titleLabelHeightSize.height : 24;

CGSize areaLabelHeightSize = [agenda.program_hal sizeWithFont: [UIFont fontWithName:@"Helvetica" size:16.0f]
                                  constrainedToSize:maximumSize
                                      lineBreakMode:NSLineBreakByWordWrapping];
cellHeight += areaLabelHeightSize.height > 21 ? areaLabelHeightSize.height : 21;

CGSize timinglLabelHeightSize = [[NSString stringWithFormat:@"%@ - %@", agenda.pg_start_time, agenda.pg_end_time] sizeWithFont: [UIFont fontWithName:@"Helvetica" size:16.0f]
                                  constrainedToSize:maximumSize
                                      lineBreakMode:NSLineBreakByWordWrapping];
cellHeight += timinglLabelHeightSize.height > 21 ? timinglLabelHeightSize.height : 21;
return cellHeight +20;

}

您可以更多地概括此功能,但我认为这足以满足您的需求。