0

我正在尝试动态更改高度,但不知何故它不起作用。“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;
}
4

1 回答 1

0

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

于 2013-01-09T07:04:15.183 回答