Ios 设置UITableViewCell高度和单元格';基于文本高度的文本标签

Ios 设置UITableViewCell高度和单元格';基于文本高度的文本标签,ios,uitableview,height,label,uilabel,Ios,Uitableview,Height,Label,Uilabel,我已经按照在stackoverflow上找到的说明修复了以下问题,但没有一个有效。下面是我的代码: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"DoCCell"; DoCCell *cell = [tableView dequeueReusableCellWi

我已经按照在stackoverflow上找到的说明修复了以下问题,但没有一个有效。下面是我的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

static NSString *CellIdentifier = @"DoCCell";
DoCCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[DoCCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell...

CGSize constraintSize = CGSizeMake(cell.infoLabel.frame.size.width, MAXFLOAT);
CGSize labelSize = [_content[[indexPath row] * 2] sizeWithFont:[UIFont fontWithName:@"HelveticaNeue-Light" size:12.0f]
                                             constrainedToSize:constraintSize
                                                 lineBreakMode:NSLineBreakByWordWrapping];
CGRect frame = CGRectMake (cell.infoLabel.frame.origin.x, cell.infoLabel.frame.origin.y, labelSize.width, labelSize.height);
[cell.infoLabel setFrame:frame];

cell.infoLabel.lineBreakMode = NSLineBreakByWordWrapping;
cell.infoLabel.numberOfLines = 10;

_font = cell.infoLabel.font;
cell.infoLabel.text = _content[[indexPath row] * 2];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
CGSize constraintSize = CGSizeMake(280.0f, MAXFLOAT);

CGSize size = [_content[[indexPath row] * 2] sizeWithFont:[UIFont fontWithName:@"HelveticaNeue-Light" size:12.0f]
               constrainedToSize:constraintSize
                   lineBreakMode:NSLineBreakByWordWrapping];
return size.height + 30.0;
}
但是,当我运行代码时,单元格的高度会相应地改变,而标签的大小则不会改变


该单元格是一个自定义单元格,我已通过.xib文件添加了标签。我尝试手动拉伸标签,其工作原理是显示所有文本,因此问题不在标签的包装中。我还测试了cell.infoLabel.frame.size.height,就值而言,高度值确实会随着单元格的高度而变化,但不会这样显示。我做错了什么?

我认为问题可能在于调整UILabel实例中文本的垂直对齐方式

我认为您应该在cellForRowAtIndexPath中执行以下操作:

cell.infoLabel.text = _content[[indexPath row] * 2];
cell.infoLabel.numberOfLines = 0;
[cell.infoLabel sizeToFit];
有关详细信息,请访问以下链接:


希望这有帮助!:)

在tableView的cellForRowAtIndexPath委托中使用以下代码:

    // getDynamicHeight function calculates height based on text length
    //Dynamically determine height for cell text
    CGFloat calculatedHeight = [self getDynamicHeight:@"Some very long text here"];  

    //Set name label frame based on text height
    cell.infoLabel.frame = CGRectMake(cell.infoLabel.frame.origin.x,cell.infoLabel.frame.origin.y, cell.infoLabel.frame.size.width,calculatedHeight);
    cell.infoLabel.numberOfLines = 5;

    cell.infoLabel.text = @"Some very long text here";

// getDynamicHeight function
//Dynamically determine height for cell based in containing Text 
-(CGFloat) getDynamicHeight : (NSString *) strCellTextName {

UILabel *lblGetDynamicHeight = [[UILabel alloc] init];
lblGetDynamicHeight.lineBreakMode = UILineBreakModeWordWrap;

lblGetDynamicHeight.text = strCellTextName;

CGSize labelStringSize = [lblGetDynamicHeight.text sizeWithFont:lblGetDynamicHeight.font constrainedToSize:CGSizeMake(142, 9999) lineBreakMode:lblGetDynamicHeight.lineBreakMode];

return  labelStringSize.height; }

好的,我现在有了`//配置单元格_font=cell.infoLabel.font;cell.infoLabel.text=_content[[indexPath行]*2];cell.infoLabel.numberOfLines=0;[cell.infoLabel sizeToFit];`但是它仍然不起作用我不确定,但是你能尝试删除标签的换行符模式吗?具体来说,你能注释掉这一行并再次检查:cell.infoLabel.lineBreakMode=NSLineBreakByWordWrapping;那也没用。是.xib中标签的详细信息,如果helps@BryceLanglotz只需将行数从10更改为0即可。这可能已被我们在代码中编写的内容覆盖,但仍然不起作用。我已经尝试了从其他帖子中我能想到的所有变化,我完全迷路了。