UITableViewCell自动调整多个UILabel的大小

时间:2014-02-08 17:00:31

标签: objective-c uitableview uilabel

我们目前正在调整UITabView中UILabel的高度。现在,客户端希望根据内容调整UITableViewCell中包含的两个标签。研究这个问题,我们看到各处都有解决方案来调整单个标签的大小,但没有关于多个标有人能指出我们为这个问题找到一个好的资源吗?

1 个答案:

答案 0 :(得分:0)

您调整标签大小的研究是正确的,您需要编写仅在一个标签上调整大小的逻辑。如果你看到了

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

其返回类型为uitableviewcell。它总是为单元格返回相同的配置(除非您为每一行创建静态单元格)。

[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

这种方法使细胞和细胞出列。重用它。所以你需要正确的逻辑来调整你创建单元格的标签的大小。或者更好地纠正自定义单元类中的调整大小逻辑。我正在添加代码,这可以帮助我解决这个问题。

CGSize maximumLabelSize = CGSizeMake(270, FLT_MAX);
CGSize expectedLabelSize = [myString sizeWithFont:[UIFont fontWithName:@"Helvetica"  size:16.0f] constrainedToSize:maximumLabelSize lineBreakMode:NSLineBreakByWordWrapping];

NSLog(@"Expected Size of label based on string--%@",NSStringFromCGSize(expectedLabelSize) );
//adjust the label the the new height.
CGRect newFrame = myLabel.frame;//get initial frame of your label
newFrame.size.height = expectedLabelSize.height;
myLabel.frame = newFrame;
NSLog(@"new frame of label--%@",NSStringFromCGRect(newFrame));
[myLabel setText:attString];
相关问题