简体   繁体   English

基于标签内容的ios7故事板动态UITableView单元格高度

[英]ios7 storyboard dynamic UITableView cell height based on label content

I am attempting to implement dynamic UITableViewCell heights based on the content of certain label in the cell. 我正在尝试根据单元格中某些标签的内容实现动态UITableViewCell高度。 The cell has a few other UI objects added to it (an image view, a few static labels etc) and I have set constraints for everything. 该单元格还添加了一些其他UI对象(图像视图,一些静态标签等),并且我为所有内容设置了约束。 Only one label needs to be sized dynamically. 仅需要动态调整一个标签的大小。

Im using this method to find the size of the text: 我使用这种方法来查找文本的大小:

-(CGFloat)getLabelHeightForText:(NSString *)text andWidth:(CGFloat)labelWidth
{
    CGSize maximumSize = CGSizeMake(labelWidth, 10000);

    UIFont *systemFont = [UIFont systemFontOfSize:15.0];
                               lineBreakMode:NSLineBreakByWordWrapping];

    CGSize labelSize = [text boundingRectWithSize:maximumSize options: NSStringDrawingUsesLineFragmentOrigin
                                       attributes: @{ NSFontAttributeName: systemFont } context: nil].size;
    return labelSize.height;
}

im sure there is a better way to do that, but it works for now. 我肯定有更好的方法可以做到这一点,但目前为止有效。

in cellForRowAtIndexPath im resizing the label to be able to hold the text and setting the appropriate attributes: 在cellForRowAtIndexPath中,将标签的大小调整为能够容纳文本并设置适当的属性:

UILabel *reviewLabel = (UILabel *)[cell viewWithTag:102];

    //280 is the width of the label
    reviewLabel.preferredMaxLayoutWidth = 280;

    CGFloat reviewLabelHeight = [self getLabelHeightForText:reviewText andWidth:reviewLabel.frame.size.width];

    reviewLabel.frame = CGRectMake(reviewLabel.frame.origin.x, reviewLabel.frame.origin.y, reviewLabel.frame.size.width, reviewLabelHeight);

    reviewLabel.text = reviewText;

    reviewLabel.numberOfLines = 0;

    reviewLabel.lineBreakMode = NSLineBreakByWordWrapping;

and that works fine, but im having trouble resizing the actual cell height. 并且效果很好,但无法调整实际像元高度的大小。 right now I have 现在我有

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

NSString *reviewText = [review objectForKey:@"reviewText"];

//280 is the width of the review label
CGFloat textHeight = [self getLabelHeightForText:reviewText andWidth:280]; 
}

but im not sure how to resize the cell using that text height. 但我不确定如何使用该文本高度来调整单元格的大小。 The cells are normally 91 px high if the text length is small enough to not need to be resized. 如果文本长度足够小而无需调整大小,则单元格通常为91 px高。 So Ive got to return 91 if the cell doesnt need to be resized, or if it does need resizing return the text height + enough pixels to fit all the other objects in the cell? 因此,如果单元格不需要调整大小,或者如果确实需要调整大小,我必须返回91,以返回文本高度+足够的像素以适合单元格中的所有其他对象? Im very confused as to how to resize the cell. 我对如何调整单元格的大小非常困惑。 The label is resizing fine, but cant get the cell figured out. 标签可以调整大小,但无法弄清楚单元格。

As you are using ios7+, try 在使用ios7 +时,请尝试

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath

instead of tableView:heightForRowAtIndexPath . 而不是tableView:heightForRowAtIndexPath

And return a CGFloat with the height offset that you want. 并返回具有所需高度偏移量的CGFloat。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM