Xcode 动态UITableViewCells高度更改

Xcode 动态UITableViewCells高度更改,xcode,uitableview,uilabel,Xcode,Uitableview,Uilabel,我试图动态地改变高度,但不知何故它不起作用。“detailText”标签与列表中的其他字段重叠。整个单元格的默认高度为150,包括用户、文本和日期。用户和日期各包含一行,其余包含文本。 我正要完成这个应用程序,但这真的打乱了整个过程,我还检查了其他技术,但不知怎么的,它们都不起作用。也许这是我错过的一件小事( 和连接器 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NS

我试图动态地改变高度,但不知何故它不起作用。“detailText”标签与列表中的其他字段重叠。整个单元格的默认高度为150,包括用户、文本和日期。用户和日期各包含一行,其余包含文本。 我正要完成这个应用程序,但这真的打乱了整个过程,我还检查了其他技术,但不知怎么的,它们都不起作用。也许这是我错过的一件小事(

和连接器

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

正如demosten所建议的,请确保在
讨论CustomCell
中为标签提供了正确的偏移量。另外,请注意,当您返回单元格的高度时,只需将标签的高度和每个标签之间的偏移量差相加。否则,您的标签可能会出现在单元格外。
例如,如果用户标签的高度为40,时间标签的高度为40。那么单元格的总高度应该是80+。希望你能理解这一点。

你的
discussionCustomCell
类中是否有正确的
layoutSubview
代码?如果它们重叠,则错误应该存在。你在计算时是否考虑了这些文本大小名称标签的框架和其他字段?我知道有一个错误,但我无法得到它,检查了大小,检查了布局,我缺少了一些东西。我认为您应该在问题中添加
discussionCustomCell
layoutSubview
代码。是的,我已经放置了正确的偏移,高度是calc计算正确。问题仍然存在,前两个是完美的位置,但之后所有的位置都是重叠的:(前两个意思-前两行或前两个标签?你有屏幕截图分享吗?
        - (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;
}