objective-c - 如何根据 NSString 计算动态大小的 UITableViewCells 所需的高度

标签 objective-c uitableview

我有一个 UITableView,它有自定义的 UITableViewCells,这是来自用户的评论。现在,我的单元格高度为 115.0f,但我希望根据评论的长度来更改高度。如果评论超过三行,我希望用户能够选择单元格,然后展开单元格以显示整个评论。我一直在使用 [UIView animateWithDuration: completion: 方法来扩展单元格,但我不知道如何根据文本的长度计算出单元格的正确大小。有人可以帮我吗?这是一些代码:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{    
    if (indexPath.row == 0)
    {
        return 480;
    }
    else
    {
        if (self.cellType == 0)
        {   
            return 115;
        }
        else
        {
            return 75;
        }
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row > 0)
    {
        NSIndexPath *path = [NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section];
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:path];
        if ([cell isKindOfClass:[CommentCell class]])
        {
            CommentCell *cell = (CommentCell *)[tableView cellForRowAtIndexPath:indexPath];
            UILabel *label = cell.commentBodyLabel;
            NSString *theText = label.text;
            CGSize constraintSize = CGSizeMake(label.frame.size.width, label.frame.size.height);
            CGSize labelSize = [theText sizeWithFont:label.font constrainedToSize:constraintSize lineBreakMode:label.lineBreakMode];
            CGFloat labelHeight = labelSize.height;
            int numLines = (int)(labelHeight/label.font.leading);
            NSLog(@"there are %i lines", numLines);
            NSLog(@"The text height is %f", labelHeight);
            if (numLines == 3)
            {
                //This is where I should expand the cell
            }
        }

最佳答案

查看 NSString UIKit Additions

您对 sizeWithFont:constrainedToSize:lineBreakMode: 特别感兴趣

将 constrainedToSize 属性的 CGSize.width 设置为单元格/标签区域的宽度。然后将 CGSize.height 设置为一个非常大的数字,可能是 CGFLOAT_MAX。这个想法是你在说“嘿,这个标签必须适合一个静态宽度的区域,但它可以永远垂直放置。所以,告诉我这个标签实际上有多高,我给你的信息”

NSString *comment = @"Some really long comment that does not fit in the standard cell size. This comment will be wrapped by word. Some more words to make this longer...";

CGSize renderedSize = [comment sizeWithFont:myFont constrainedToSize:CGSizeMake(kCellWidth, CGFLOAT_MAX) lineBreakMode:NSLineBreakByWordWrapping];

renderedSize.height 是您将为 (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

返回的值

关于objective-c - 如何根据 NSString 计算动态大小的 UITableViewCells 所需的高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16864430/

相关文章:

ios - 如何到达发件人的父 View ?

iphone - 如何在 MKMapView 中关闭 MKAnnotation 的标注

c++ - 数组,复制像素到正确的索引,算法

ios - 我的 NSUserDefaults 代码不起作用

swift - UITableview 单元格值不会 swift 改变

ios - 使用 UITableViewController 比使用实现 tableview 委托(delegate)和数据源方法的 UIViewController 有什么优势吗?

uitableview - 单击 UIAlertView 按钮时更改 View

ios - 禁用特定 UITableView 部分的分隔符

objective-c - 创建可自定义 uiview 的正确方法

ios - Swift UITableView 删除所选行及其上方的任何行