首页 文章

表格视图中UILabel的细胞动态高度

提问于
浏览
2

我正在开发一个聊天应用程序,显示用户的消息和图像 . 我有一个表视图和一个自定义单元格 . 单元格有一个UIlabel和UIImage . 我尝试了各种方法来根据标签的内容文本动态调整单元格高度 . 我在故事板本身设置了 numberofLines=0 并将单元格的高度设置为常量,以便可以容纳多行 . 但它不显示多行和至于身高,我使用了自动尺寸,但它也不起作用 . 我还使用了以下link我正在使用messageTableViewCell类型的自定义单元格,其中包含标签属性 .

这是快照: -
image

我在viewcontroller.m中的代码

- (void)viewDidLoad
{

   self.tableView.estimatedRowHeight = 100.0;

   self.tableView.rowHeight = UITableViewAutomaticDimension;
}

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

     messageTableViewCell *cell = [self.messageTableView dequeueReusableCellWithIdentifier:@"cell"];

    if (cell == nil) 
    {
         cell = [[messageTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }

    cell.messageLabel.text = [_messages objectAtIndex:indexPath.row];
    cell.messageLabel.lineBreakMode = NSLineBreakByWordWrapping;
    cell.messageLabel.numberOfLines=0;
    [cell.messageLabel sizeToFit];
    return cell;
}

EDIT

而不是标签我在单元格中插入了一个textview并将其修改为:

- (void)textViewDidChange:(UITextView *)textView
{
    CGFloat fixedWidth = textView.frame.size.width;
    CGSize newSize = [textView sizeThatFits:CGSizeMake(fixedWidth,     MAXFLOAT)];
    CGRect newFrame = textView.frame;
    newFrame.size = CGSizeMake(fmaxf(newSize.width, fixedWidth),   newSize.height);
    textView.frame = newFrame;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath: (NSIndexPath *)indexPath
{
    static messageTableViewCell *sizingCell = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sizingCell = [self.messageTableView  dequeueReusableCellWithIdentifier:@"cell"];
   });

    sizingCell.textMessage.text=_messages[indexPath.row];
    CGFloat fixedWidth = sizingCell.textMessage.frame.size.width;
    CGSize newSize = [sizingCell.textMessage     sizeThatFits:CGSizeMake(fixedWidth, MAXFLOAT)];
    CGRect newFrame = sizingCell.textMessage.frame;
    newFrame.size = CGSizeMake(fmaxf(newSize.width, fixedWidth), newSize.height);
    CGFloat height= newFrame.size.height;
    NSLog(@"%f is height ",height);
    return height+5.0f;
}

但它也没有用,它切断了文本的上半部分 .

4 回答

  • 4

    如果您正在iOS 8上面工作,请尝试此操作 . 它适用于我 .

    cell.messageLabel.text = [_messages objectAtIndex:indexPath.row];
    cell.messageLabel.lineBreakMode = NSLineBreakByTruncatingTail;
    cell.messageLabel.numberOfLines=0;
    return cell;
    
    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        return UITableViewAutomaticDimension;
    }
    - (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
        return 10.0;
    }
    
  • 5

    http://www.raywenderlich.com/73602/dynamic-table-view-cell-height-auto-layout

    这可能是我遇到的关于这个主题的最好的教程之一 . 它会逐步解释所有内容,以便您可以根据自己的需要调整内容 .

  • 3

    试试这个:

    - (float)getMessageSize:(NSString *)text Width:(float)textWidth
    {
        NSAttributedString *attributedText = [[NSAttributedString alloc] initWithString:text
            attributes:@{
                NSFontAttributeName: [UIFont fontWithName:fontNameRefrig size:19.0]
            }];
    
        CGRect rect = [attributedText boundingRectWithSize:(CGSize){textWidth, CGFLOAT_MAX}
            options:NSStringDrawingUsesLineFragmentOrigin
            context:nil];
        CGSize finalSize = rect.size;
        return finalSize.height;
    }
    
  • 0

    试试这段代码

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return [customcell getCellHeightForText:@"text"];
    }
    
    
     - ( UITableViewCell* ) tableView : ( UITableView* ) tableView cellForRowAtIndexPath : ( NSIndexPath* ) indexPath {
    
     cell = [ tableView dequeueReusableCellWithIdentifier : @"customCell" ] ;
      CGRect  rect=cell.frame;
      rect.size.height = [customcell getCellHeightForText:@"text"];
     cell.frame=rect;
    return cell;
    }
    

    在customcell.m文件上

    +(NSInteger) getCellHeightForText:(NSString *) text {
    float totalHeight = 0;
    float topMargin = 35;
    float bottomMargin = 30;
    CGSize textSize = [text calculateSize:12.0 maxWidth:195 maxHeight:0 lineBreakMode:NSLineBreakByWordWrapping];
    totalHeight = totalHeight + textSize.height + topMargin + bottomMargin;
    return totalHeight;
    }
    

相关问题