简体   繁体   English

TableView单元格自动高度

[英]TableView Cell Auto Height

I have a TextView in the UITableViewCell , and I want my height TableViewCell is auto according TextView height. 我在UITableViewCell有一个TextView,我希望我的高度TableViewCell根据TextView的高度是自动的。

I had worked some code, but it display on the IOS 8 only, I want it can action on the IOS 7. 我已经编写了一些代码,但是只在IOS 8上显示,我希望它可以在IOS 7上执行。

Please help me! 请帮我!

Thanks all. 谢谢大家

Here is image my code, it work on the IOS 8 very good. 这是我的代码,可以在IOS 8上很好地工作。

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

    NSString *messageText = [NSString stringWithFormat:@"%@", [DicMessagesData objectForKey:@"ContentMessage"]];

    CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);

    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;

    CGRect textRect = [messageText boundingRectWithSize:constraint options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSParagraphStyleAttributeName: paragraphStyle.copy} context:nil];

    return textRect.size.height + 30;

}

the best method i have found : 我发现的最好的方法:

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

  NSString *text = @"your text";
  CGSize maximumLabelSize = CGSizeMake(CELL_CONTENT_WIDTH, 9999);

  UILabel *notesLabel = [[UILabel alloc] init];
  notesLabel.font = [UIFont systemFontOfSize:18];
  notesLabel.text = text;  
  notesLabel.numberOfLines = 0;
  notesLabel.lineBreakMode = NSLineBreakByWordWrapping;
  CGSize expectSize = [notesLabel sizeThatFits:maximumLabelSize];
  return expectSize.height + 40;
}

A few months ago I faced the same problem. 几个月前,我遇到了同样的问题。 Thankfully I found a simple blog on Table View Cells With Varying Row Heights . 值得庆幸的是,我在有关行高变化的表视图单元格上找到了一个简单的博客。 It will solve your problem. 它将解决您的问题。

Try this. 尝试这个。 It works. 有用。 This will give dynamic height for each tableview cell based on the content. 这将根据内容为每个tableview单元提供动态高度。

- (CGFloat)heightForText:(NSString *)bodyText
{
    NSAttributedString *attrStr = [[NSAttributedString alloc] initWithString:bodyText];
    CGFloat width = 278;
    CGRect rect = [attrStr boundingRectWithSize:CGSizeMake(width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading context:nil];
    return rect.size.height+10;

} }

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    lblComment.numberOfLines = 0;
    lblComment.lineBreakMode = NSLineBreakByWordWrapping;
    CGFloat rowHeight = [self heightForText:[[allCommentsArr objectAtIndex:indexPath.row]objectForKey:@"comment"]];
    lblComment.frame = CGRectMake(25, 25, 278, rowHeight);
    NSLog(@"rowHeight=%f", rowHeight);
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
        CGFloat rowHgt=[self heightForText:[[allCommentsArr objectAtIndex:indexPath.row]objectForKey:@"comment"]];
        NSLog(@"rowHgt=%f", rowHgt);
        return rowHgt+25;
}

在此处输入图片说明

you can use this code: 您可以使用以下代码:

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

  NSString *text = @"your text";
  CGSize maximumLabelSize = CGSizeMake(CELL_CONTENT_WIDTH, 9999);

  UITextField *textField = [[UITextField alloc] init];
  textField.font = [UIFont systemFontOfSize:18];
  textField.text = text;
  CGSize expectSize = [textField sizeThatFits:maximumTXTSize];
  return expectSize.height + 40;
}

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

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