动态UITableViewCells高度变化

时间:2013-01-08 22:42:09

标签: xcode uitableview uilabel

我试图动态改变高度但不知何故它不起作用。 “detailText”标签与列表的其他字段重叠。整个单元格的默认高度为150,包括用户,文本,日期。用户和日期各占一行,其余为文本。 我即将完成应用程序,但这确实扰乱了整个过程,我还检查了其他技术,但不知何故,它们都没有工作。可能是我失踪的一件小事。 :(

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    NSString *user=[[discussionArray objectAtIndex:indexPath.row] objectForKey:@"user"];
    NSString *text=[[discussionArray objectAtIndex:indexPath.row] objectForKey:@"text"];
    NSString *time=[[discussionArray objectAtIndex:indexPath.row] objectForKey:@"date"];
    CGSize usersize = [user sizeWithFont:[UIFont systemFontOfSize:15] constrainedToSize:CGSizeMake(250, MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap];
    CGSize textsize = [text sizeWithFont:[UIFont systemFontOfSize:13] constrainedToSize:CGSizeMake(250, MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap];
    CGSize timesize = [time sizeWithFont:[UIFont systemFontOfSize:12] constrainedToSize:CGSizeMake(250, MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap];

    return usersize.height+textsize.height+timesize.height;
}

和连接器

        - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellIdentifier = @"Cell";
    discussionCustomCell *cell = (discussionCustomCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"discussionCustomCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }

    NSLog(@"%i",[discussionArray count]);
    NSDictionary *dict=[discussionArray objectAtIndex:indexPath.row];
    cell.imageViewIst.image = [profileImagesArray_ objectAtIndex:indexPath.row];
    NSString *new=[dict objectForKey:@"new"];
    if ([new isEqualToString:@"0"])
    {
    cell.imageViewSecond.backgroundColor=[UIColor redColor];
    }
    else
    {
    cell.imageViewSecond.backgroundColor=[UIColor greenColor];
    }
    cell.nameLabel.text=[dict objectForKey:@"user"];
    cell.nameLabel.font = [UIFont boldSystemFontOfSize:15];
    cell.nameLabel.numberOfLines = ceilf([[dict objectForKey:@"user"] sizeWithFont:[UIFont systemFontOfSize:15] constrainedToSize:CGSizeMake(250, MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap].height/20.0);
    cell.detailText.text=[dict objectForKey:@"text"];
    cell.detailText.font = [UIFont boldSystemFontOfSize:15];
    cell.detailText.numberOfLines = ceilf([[dict objectForKey:@"text"] sizeWithFont:[UIFont systemFontOfSize:13] constrainedToSize:CGSizeMake(250, MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap].height/20.0);
    cell.timeLabel.text=[dict objectForKey:@"date"];
    cell.timeLabel.font = [UIFont boldSystemFontOfSize:15];
    cell.timeLabel.numberOfLines = ceilf([[dict objectForKey:@"date"] sizeWithFont:[UIFont systemFontOfSize:12] constrainedToSize:CGSizeMake(250, MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap].height/20.0);
    return cell;
}

1 个答案:

答案 0 :(得分:0)

正如demosten建议的那样,请确保在discussionCustomCell中为标签指定正确的偏移量。另请注意,当您返回单元格的高度时,只需添加标签的高度和每个标签之间的偏移差异。否则,您的标签可能会出现在单元格之外。 即。如果用户标签的高度为40,时间标签的高度为40.那么总的单元格高度应为80+。希望你明白这一点。