0

I am working on Tableview with dynamic cell height.

Due to my requirement, i have to show 40 types of different cell.Each cell is different.

For ex.

one cell having 2 labels,1 imageview another cell having only 2 imageview another cell having 5 labels,1 textview,2 buttons, 2 imageview

My problem is i cant able to define the height statically.ie. labels and textview in each cell is based on the text at run time.So according to the individual elements i need to adjust the height.So i have calculated the label and textview height based on below coding,

 CGSize constrainedSize = CGSizeMake((IS_IPAD)?765:285  , 9999);

 NSDictionary *dict=[array objectAtIndex:indexPath.row];

 NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
 [UIFont fontWithName:@"Lucida Sans" size:(IS_IPAD)?18.0:12.0], NSFontAttributeName,
 nil];

 NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:[dict valueForKey:@"response"]attributes:attributesDictionary];

 CGRect requiredHeight = [string boundingRectWithSize:constrainedSize options:NSStringDrawingUsesLineFragmentOrigin context:nil];

 return requiredHeight.size.height;

The above code works fine.But it's little bit difficult to calculate for 5-10 labels for a cell.Because i need to call 10 times for every cell.So it causes slow scrolling on tableview.Also after that i need to change every label/textview and imageview's y position frame.Is there any alternate to achieve this? Any help would be highly appreciated.

Thanks.

Thangavel
  • 216
  • 2
  • 10
  • Add one more Property in your `NSDictionary` with value of height, add height when you enter data in `NSDictionary` with calculation. This will let down memory usage of calculating height every-time when cell is generated or displays, – Viral Savaj May 06 '15 at 09:59
  • Refer to [Dynamic Height Issue for UITableView Cells (Swift)](https://stackoverflow.com/q/30494702/6521116) – LF00 Mar 12 '18 at 09:26

1 Answers1

0

Initially set a placeholder height for all your cells. later try to reload cells with exact heights.

Write a method that is capable of calculating the approx height for a specific cell. And pass some parameters into it.

Abdul Yasin
  • 3,480
  • 1
  • 28
  • 42