0

I have problem with UITableViewCell, I want to make the dynamical height of each cell, I tried code bellow:

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([[tableView cellForRowAtIndexPath:indexPath] isKindOfClass:[CommentTableViewCell class]]) {
        CommentTableViewCell* cell = (CommentTableViewCell*)[tableView cellForRowAtIndexPath:indexPath];
        return cell.descriptionText.frame.size.height + 20.0;
    }
   return 44.0;
}

But I have error, I have screenshot of this error https://drive.google.com/file/d/0Bz4j1JqQ0tI4T1Jsb2YxWnBvSTA/view?usp=sharing

Then I use answer of Mohit tomar in this page: Change UITableView height dynamically

In result of it: https://drive.google.com/file/d/0Bz4j1JqQ0tI4R0t4M0k5THdxNEk/view?usp=sharing

Community
  • 1
  • 1
user3724714
  • 31
  • 1
  • 5
  • Can you please add code where you're creating cell, `cellForRowAtIndexPath`. – Mrunal Jul 16 '15 at 06:25
  • 1
    Google? Stackoverflow search? Auto layout & `tableView.rowHeight = UITableViewAutomaticDimension` are your friends. Example in [Self Sizing Table View Cells](http://useyourloaf.com/blog/2014/08/07/self-sizing-table-view-cells.html) article and [sample project](https://github.com/kharrison/CodeExamples/tree/master/SelfSize/SelfSize). – zrzka Jul 16 '15 at 06:31
  • possible duplicate of [Using Auto Layout in UITableView for dynamic cell layouts & variable row heights](http://stackoverflow.com/questions/18746929/using-auto-layout-in-uitableview-for-dynamic-cell-layouts-variable-row-heights) – zrzka Jul 16 '15 at 06:34
  • What is the content of each cell? – abhimuralidharan Jul 16 '15 at 06:44

3 Answers3

0

UITableView get cell height in first,then create cell use cellForRowAtIndexPath method.

So you can't use

CommentTableViewCell* cell = (CommentTableViewCell*)[tableView cellForRowAtIndexPath:indexPath];
  return cell.descriptionText.frame.size.height + 20.0;

To get cell height. in heightForRowAtIndexPath: method ,you must calculate each cell's height for the value of return .

ketan
  • 19,129
  • 42
  • 60
  • 98
Ninjajun
  • 1
  • 1
  • 2
0

If you are targeting iOS ≥ 8 you can use dynamic cell height:

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

Nevertheless, you should also provide tableView:estimatedHeightForRowAtIndexPath: and return eater 44 or the cells approx. height.

Please also verify your constraints. Your second screenshot implies that you don't have a correct bottom constraint or the priorities are incorrect.

The SO-Answer you mentioned is absolutely right, again, only iOS8+

Julian F. Weinert
  • 7,474
  • 7
  • 59
  • 107
0

In your tableview's heightForRowAtIndexPath you have to calculate the size of the text and return the "height" parameter of that size.

You can maintain a function as below to calculate the size of the text

- (CGSize)heightForText:(NSString *)bodyText
{
    CGRect rect = CGRectZero;
    CGSize size;
    UIFont *font = [UIFont fontWithName:@"Corbel" size:14.0];
    float iosVersion = [[[UIDevice currentDevice] systemVersion] floatValue];

   rect = [bodyText boundingRectWithSize:CGSizeMake(240, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:font} context:nil];

    size = CGSizeMake(rect.size.width, rect.size.height);
    return size;
}

and the in your "heightForRowAtIndexPath" use it as :

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

  CGSize Size = [self heightForText:yourText];
  return Size.height
}

This should resize your cell properly

Shailesh
  • 3,072
  • 3
  • 24
  • 33