Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableView jumps when expanding cell with autolayout. [duplicate]

I've been searching and can't find a solution to this problem. My current code is based on Reload section without reloading section header.

I'm trying to grow a uitableview cell when the user presses into the cell. I have a UITableViewCell subclass with a UILabel that is initially limited to two lines of text. When the the user presses the UILabel I set the numberOfLines property in the UILabel to 0 so it expands to as many lines as needed.

Here is my code from tableView:didSelectRowAtIndexPath:

if (((NSNumber *)expandedCells[indexPath.section]).boolValue)
{
     labelExpandingCell.cellLabel.numberOfLines = 10;
} else
{
     labelExpandingCell.cellLabel.numberOfLines = 3;
}
[UIView setAnimationsEnabled:NO];
[self.tableView beginUpdates];
[self.tableView setNeedsDisplay];
[self.tableView endUpdates];
[UIView setAnimationsEnabled:YES];

Sometimes this works fine, typically when the UITableView's contentOffset.y == 0. Usually the tableview jumps to the backwards 100 pixels or so when I execute this code. Here is an example log from before and after this code is ran.

2016-03-25 14:57:08.549 TableViewTest[485:129926] Content offset before update: 153.500000 2016-03-25 14:57:08.568 TableViewTest[485:129926] Content offset after update: 136.500000

Why would autolayout be causing my tableview to jump backwards when I expand the currently visible cell?

like image 620
Corey Zambonie Avatar asked Mar 12 '23 19:03

Corey Zambonie


1 Answers

I fixed this issue by implementing the estimatedHeightForRowAtIndexPath method in my tableview's delegate. I believe the issue was with the tableview not properly estimating the height of the previous sections in my tableview without this method implemented, thus jumping when I'd update a section in the tableview.

like image 78
Corey Zambonie Avatar answered May 01 '23 07:05

Corey Zambonie