4

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?

Community
  • 1
  • 1
Corey Zambonie
  • 614
  • 5
  • 17
  • I'm getting something similar with code that used to work. Not sure when this started. iOS 9 sdk possibly? Sure don't wanna go back to setting cell heights manually!! – tom-pratt Mar 26 '16 at 15:51

2 Answers2

6

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.

Corey Zambonie
  • 614
  • 5
  • 17
1

Have you set the UITableView

tableView.rowHeight = UITableViewAutomaticDimension tableView.estimatedRowHeight = DEFAULT_ROW_HEIGHT

If yes, check ensure that your UILabel constraints are correctly applied to cell. You can avoid implementing "estimatedHeightForRowAtIndexPath" method if you set the above two properties.

Manoj
  • 1,019
  • 9
  • 17