简体   繁体   中英

How to change each UITableViewCell height dynamically?

I'm working on application where i show user comment in UILable and UILable have sizetofit property. i want to change cell height according to UILable height.

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.

For dynamic height of UITableViewCell you have to do below things

  • Fulfill all constraint requirement in UITableViewCell
  • Tell your TableView to dynamically layout Height of every Cell with below code

     - (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)

- (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:

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

In your heightForRowAtIndexPath, calculate the dynamic height based on the related cell data.

- (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. And your tableViewCell defined a UILabel called contentLabel with CONTENT_LABEL_WIDTH

+(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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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