micnguyen
micnguyen

Reputation: 1449

UITableView beginUpdate/endUpdate causing scroll to top

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

Upvotes: 21

Views: 8507

Answers (2)

Joshua Haines
Joshua Haines

Reputation: 349

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.

Upvotes: 6

akiraspeirs
akiraspeirs

Reputation: 2135

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;
}

Upvotes: 36

Related Questions