首次出现时不能更改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
通过在单元格的top
和bottom
在UILabel
上具有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:
。 这个逻辑适用于所有的iOS5
, iOS6
, iOS7
。
-(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; }
如果您使用自动布局,修改框架将不起作用。
相反,你应该修改约束。
- 使用自动布局在UIScrollView中使用浮动视图滚动犹豫
- UITableView tableFooterView显示在UITableView的顶部 – 错误
- animationUIStackView排列的子视图内容大小更改
- 如何使用自动布局以编程方式将活动指标定位到其超级视图的中心?
- 仅UIScrollView自动布局垂直
- 将约束添加到xib中的“主视图”
- 是否有可能在UIScrollView放大和缩小UIImageView,但坚持自动布局居中?
- 表视图单元从View Controller返回到Table View Controller时“跳转”
- 使用自动布局pipe理UITextField的dynamic位置