简体   繁体   English

如何动态更改每个UITableViewCell高度?

[英]How to change each UITableViewCell height dynamically?

I'm working on application where i show user comment in UILable and UILable have sizetofit property. 我正在开发应用程序,我在UILable和UILable中显示用户评论有sizetofit属性。 i want to change cell height according to UILable height. 我想根据UILable高度改变单元格高度。

My Question is how i change cell height for example first cell height may be 50, second Cell height may be 100 and so on. 我的问题是我如何改变单元格高度,例如第一个单元格高度可能是50,第二个单元格高度可能是100,依此类推。

For dynamic height of UITableViewCell you have to do below things 对于UITableViewCell的动态高度,你必须做下面的事情

  • Fulfill all constraint requirement in UITableViewCell 满足UITableViewCell中的所有约束要求
  • Tell your TableView to dynamically layout Height of every Cell with below code 告诉您的TableView使用以下代码动态布局每个Cell的高度

     - (void)viewDidLoad { [super viewDidLoad]; // two magic lines tableView.estimatedRowHeight = 89 tableView.rowHeight = UITableView.automaticDimension } 

With just two lines of code, you instruct the table view to calculate the cell's size matching its content and render it dynamically. 只需两行代码,您可以指示表视图计算与其内容匹配的单元格大小并动态呈现它。 This self sizing cell feature should save you tons of code and time. 这种自定义单元格功能可以节省大量的代码和时间。 You're gonna love it. 你会爱上它的。

You can use this method for increase UITableViewCell height dynamically (No AutoLayout) 您可以使用此方法动态增加UITableViewCell高度(No AutoLayout)

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSMutableAttributedString *strName = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@",strItemName]];

    [strName addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:16] range:NSMakeRange(0, strItemName.length)];
    CGSize sizeItemName = CGRectIntegral([strName boundingRectWithSize:CGSizeMake(130, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin context:nil]).size;
    int padding = 5;
    //your default cell height for ex 55 
    if (sizeItemName.height < 55)
    {
        sizeItemName.height = 55;
    }
    return sizeItemName.height + padding;
}

Hope this helps you by tableview methods: 希望这可以通过tableview方法帮助你:

 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
 {
     return UITableViewAutomaticDimension;
 }

In your heightForRowAtIndexPath, calculate the dynamic height based on the related cell data. 在heightForRowAtIndexPath中,根据相关的单元格数据计算动态高度。

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
 { 
     NSDictionary *data = [self.dataSource objectAtIndex:indexPath.row];
     return [MyTableViewCell heightForData:data];
 }

Then in your MyTabLeViewCell, write a function as below, let us say the data has the "content" which is the fact for dynamic height. 然后在MyTabLeViewCell中编写如下函数,让我们说数据具有“内容”,这是动态高度的事实。 And your tableViewCell defined a UILabel called contentLabel with CONTENT_LABEL_WIDTH 并且你的tableViewCell使用CONTENT_LABEL_WIDTH定义了一个名为contentLabel的UILabel

+(CGFloat) heightForData : (NSDictionary *)data{

    self.contentLabel.text = [data objectForKey:@"content"];
    CGSize contentLabelSize = [self.contentLabel sizeThatFits:CGSizeMake(CONTENT_LABEL_WIDTH, CGFLOAT_MAX)];

    return contentLabelSize.height;

    //If you want to have a minimum cell height no matter how small your content is, you can use below fmaxf with a pre-defined CELL_MIN_HEIGHT value.
    // return fmaxf(CELL_MIN_HEIGHT, height);
}

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

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