21

I've been trying to get my head around this all-too common problem of a expanding UITextView inside a growing UITableViewCell of a UITableView and I'm nearly there except for one small thing.

I have a UITableView that has a custom UITableViewCell. My UITableView is using the new iOS8 self-sizing dynamic cell height with the help of self.tableView.estimatedRowHeight and self.tableView.rowHeight = UITableViewAutomaticDimension;, that grows the cell height based on it's ContentView's auto-layout.

I can properly grow my UITextField's frame and my UITableView's cell height does indeed grow, but every time I called self.tableView beginUpdates and the matching endUpdates, my entire scroll view of the tableView forces a scroll to the top of the table view. I have a method that scrolls to the caret position, but as you can imagine, every time a new line is created in the UITextView, the UITableView scrolls to the very top and then to the caret position and it's janky as hell.

Anyone have any ideas? I can provide a video if necessary. Cheers, Mike

micnguyen
  • 1,419
  • 18
  • 25
  • Possible duplicate of [Jerky Scrolling After Updating UITableViewCell in place with UITableViewAutomaticDimension](http://stackoverflow.com/questions/27996438/jerky-scrolling-after-updating-uitableviewcell-in-place-with-uitableviewautomati) – PakitoV Mar 01 '16 at 14:25

2 Answers2

36

This got me as well, and it looks like self.tableView.estimatedRowHeight is the problem. When I made the estimate large enough the problem stopped happening.

I added this instead and it's working well so far:

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewAutomaticDimension;
}
akiraspeirs
  • 2,127
  • 2
  • 21
  • 29
  • 1
    What's your setup? I made the estimatedRowHeight super large and it still displayed this behaviour. Your code sample doesn't seem to be working for me as well. – micnguyen Mar 29 '15 at 13:47
  • 9
    I used a value of 1000 and it worked. I'm apprehensive about using UITableViewAutomaticDimension since that seems to be used to indicate via an actual row height return value (tableView.rowHeight or tableView:heightForRowAtIndexPath:) that the system should use the estimatedRowHeight to size the cells before autolayout has had a chance to figure the size. Seems that it may work just by coincidence in your case. The value is -1 in the debugger and it doesn't work for me. – Andrew Raphael Sep 27 '15 at 19:21
  • 2
    This is f**king unbelievable. Thx. – Gabriel.Massana Aug 16 '16 at 16:11
  • 4
    It still scrolls to the top occasionally. – Zack Zhu Aug 30 '16 at 20:21
  • @ZackZhu Have you find a solution? – Jassi Nov 04 '16 at 10:11
  • 3
    Having both identical estimatedHeight and heightForRow methods solved the problem in my case – Centurion Nov 21 '16 at 17:33
  • Cheers, worked for me. Only seems to be a problem on iOS versions less than iOS 10, – AdamM Jul 13 '17 at 08:16
6

I can't comment on akiraspeirs answer, but setting the self.tableView.estimatedRowHeight to the exact height of my cell before any resizing happens got rid of the weird scrolling issue. His second solution didn't work in my case.

Community
  • 1
  • 1
Joshua Haines
  • 349
  • 5
  • 8