首次出现时不能更改UITableViewCell中的UILabel的框架(使用Autolayout)

我有一个UITableView中包含UILabel的原型单元格。 我想dynamic地改变标签的大小(和单元格),这取决于标签中文字的大小。

我像这样在cellForRowAtIndexPath创build原型单元格:

 static NSString *CellIdentifier = @"ProgramDetailCell"; ProgramDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; cell.descriptionLabel.text = self.program.subtitle; return cell; 

然后我的ProgramDetailCell有以下实现:

 @implementation ProgramDetailCell - (void)layoutSubviews { [super layoutSubviews]; [self.descriptionLabel sizeToFit]; } @end 

单元格第一次显示时, layoutSubviews被调用,但descriptionLabel不会被resize。 但是,如果我向下滚动表格并重新备份,则“重用”单元格将显示,并且标签的大小已正确调整!

为什么第一次显示单元格时不工作 – 我需要做些什么来修复它。

提前致谢!

在xib中,转到第一个选项卡,在Interface Builder Document下,取消选中Auto Layout框。

它不工作,因为你正在使用auto layout 。 你需要一些auto layout方式来实现这一点。

这是解决scheme。

步骤1 :

UILabel固定到UITableViewCell顶部和底部。 您可以通过Interface Builder通过在单元格的topbottomUILabel上具有Vertical Constraint来实现此目的。 如果单元格的高度增加,这将使得UILabel的高度增加,反之亦然。

第2步:

在你的UIViewController中- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath方法计算UITableViewCell的大小。

你可以使用下面的计算

  CGRect textFrame = [YourText boundingRectWithSize:CGSizeMake(width of the label, FLT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:Your Font} context:nil]; 

现在返回textFrame.size.height + padding。

第3步:

您将达到您想要的效果—即使是第一次编译和运行之后,“根据标签中的文本大小dynamic更改标签(和单元格)的大小”。

从服务器获取所有数据后,您可以使用此方法获取该UILabel高度。

 CGSize maximumSize = CGSizeMake(210, 9999); for (int i = 0; i < [_arrProductList count]; i++) { float row_height = 0.0f; ProductInformation *product_obj = [_arrProductList objectAtIndex:i]; CGSize desc_size = [self measureHeightForText:product_obj.product_desc forFont: [UIFont systemFontOfSize:14] forSize:maximumSize]; row_height = row_height + desc_size.height; // [_arrRowHeights addObject:[NSString stringWithFormat:@"%f", row_height]]; You can take it into array. } [tableView reloadData]; 

在这里我已经给出了measureHeightForText:描述measureHeightForText: 。 这个逻辑适用于所有的iOS5iOS6iOS7

 -(CGSize)measureHeightForText:(NSString *)strText forFont:(UIFont *)font forSize:(CGSize)size{ if (!testingLabel) { testingLabel = [[UILabel alloc] init]; // testingLabel.font = [UIFont fontWithName:[AppHandlers zHandler].fontName size:16]; testingLabel.text = @""; testingLabel.numberOfLines = 0; } testingLabel.text =strText; testingLabel.font = font; CGSize expectedSize = [testingLabel sizeThatFits:size]; return expectedSize; } 

然后根据它更新你的标签的大小。 这对我来说工作得很好。 我正在使用它。

因为layoutSubviews被调用时, descriptionLabel的文本还没有被设置。 而当你滚动,文本被设置。 所以现在是正确的。

我build议你在设置文本后调用sizeToFit

 static NSString *CellIdentifier = @"ProgramDetailCell"; ProgramDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; cell.descriptionLabel.text = self.program.subtitle; [cell.descriptionLabel sizeToFit]; return cell; 

heightForRowAtIndexPath调用此方法,然后手动计算高度

 static NSString *CellIdentifier = @"ProgramDetailCell"; ProgramDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; cell.descriptionLabel.text = self.program.subtitle; [cell.descriptionLabel sizeToFit]; return cell.descriptionLabel.frame.size.height+cell.descriptionLabel.frame.origin.y; 

你可以使用下面的代码:

 //Calculate the expected size based on the font and linebreak mode of your label // FLT_MAX here simply means no constraint in height CGSize maximumLabelSize = CGSizeMake(296, FLT_MAX); CGSize expectedLabelSize = [yourString sizeWithFont:yourLabel.font constrainedToSize:maximumLabelSize lineBreakMode:yourLabel.lineBreakMode]; //adjust the label the the new height. CGRect newFrame = yourLabel.frame; newFrame.size.height = expectedLabelSize.height; yourLabel.frame = newFrame; 

如果您的要求是dynamic更改标签高度平均请按照下面的链接。 当我们需要改变标签的高度时,我们也需要改变行高:

调整UILabel的大小并不完全正常

使用下面的代码来设置单元格中的dynamic高度

 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { NSString *messagetext=[NSString stringWithFormat:@"%@",[[arryStoryView objectAtIndex:indexPath.row] valueForKey:@"messagetext"]]; CGSize StoryTextSize= [messagetext sizeWithFont:[UIFont fontWithName:@"Georgia" size:17.0f] constrainedToSize:CGSizeMake(300, MAXFLOAT) lineBreakMode:NSLineBreakByWordWrapping]; int TotalHeight; TotalHeight= StoryTextSize.height + 10; return TotalHeight; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell1"; UITableViewCell *cell=[tblSendMsg dequeueReusableCellWithIdentifier:CellIdentifier]; cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; cell.selectionStyle = UITableViewCellSelectionStyleNone; NSString *storytext=[NSString stringWithFormat:@"%@",[[arryStoryView objectAtIndex:indexPath.row] valueForKey:@"storytext"]]; CGSize StoryTextSize = [storytext sizeWithFont:[UIFont fontWithName:@"Georgia" size:17.0f] constrainedToSize:CGSizeMake(300, MAXFLOAT) lineBreakMode:NSLineBreakByWordWrapping]; lblStoryText.frame=CGRectMake(5, 25, [[UIScreen mainScreen] bounds].size.width-10, StoryTextSize.height+30); int nooflines=StoryTextSize.height/16; int i= StoryTextSize.height; if(i%16 !=0) { nooflines=nooflines+1; } lblStoryText.numberOfLines=nooflines; lblStoryText.font=[UIFont fontWithName:@"Georgia" size:17.0f]; lblStoryText.text=[NSString stringWithFormat:@"%@",storytext]; return cell; } 

如果您使用自动布局,修改框架将不起作用。

相反,你应该修改约束。