I have a cell with a label, sometimes the label text changes and the cell should be expanded a little bit. I'm using auto layout and variable height cells (you know, UITableViewAutomaticDimension and estimatedRowHeight). It seems it is not working unless after modifying the label I call reloadData or reload that specific cell. Is that normal? Is there any way to do this automatically or without having to use the view controller to reload the cell that now should have another height? Thanks.
Asked
Active
Viewed 416 times
0
-
it is normal, you should calculate the space occupied by the text in the cell frame, set this value into variable, then pass this variable to: `(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath` – alessioarsuffi Feb 24 '16 at 09:43
-
The thing is, just calling reloadData or reloadRowsAtIndexPaths does the trick, no need to calculate manually any height (because I use autolayout). The problem I have is if that can be done automatically or I have to reload always manually. – Ricardo Feb 24 '16 at 09:58
-
ok, try calling `[tableView beginUpdates] [tableView endUpdates]` this should reload the cell frame based on its constraint and new text change – alessioarsuffi Feb 24 '16 at 10:11
3 Answers
1
I had a similar problem in one of my apps and what I did was add 2 constraints, let d be the default height of the label 1. height of the label >= d, priority = 750 2. height = d, priority = 1000 and uitableview cell attribute to UITableViewAutomaticDimension This worked for me.

Keerthi Polepalle
- 146
- 8
0
try this one
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([[[arrFeed objectAtIndex:indexPath.section] objectForKey:@"post_keywords"] isEqualToString:@""])
{
int heigthHeading = [self getHeightForText:[[arrFeed objectAtIndex:indexPath.section] objectForKey:@"post_heading"] withFont:[UIFont systemFontOfSize:17.0] andWidth:245.0];
return 25+heigthHeading;
}
else
{
int heigthHeading = [self getHeightForText:[[arrFeed objectAtIndex:indexPath.section] objectForKey:@"post_heading"] withFont:[UIFont systemFontOfSize:17.0] andWidth:245.0];
return 25+heigthHeading;
}
}

Birendra
- 623
- 1
- 5
- 17
0
Make cell height dynamic (You can also add multiple label on same cell like this way And your cell increased height depends on labels text)
Add Four Constraints to lable (Top, Bottom, Leading, Trailling)
Set Number Of line 0 and line break mode word wrap
Add this two delegate method
-(CGFloat)tableView:(UITableView )tableView estimatedHeightForRowAtIndexPath:(NSIndexPath )indexPath
{
return 44;
}
-(CGFloat)tableView:(UITableView )tableView heightForRowAtIndexPath:(NSIndexPath )indexPath
{
return UITableViewAutomaticDimension;
}

Shrikant Tanwade
- 1,391
- 12
- 21
-
http://stackoverflow.com/questions/35789560/dynamically-change-tableview-cell-height-swift/35789561#35789561 – Shrikant Tanwade Mar 04 '16 at 06:51