Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableView beginUpdate/endUpdate causing scroll to top

Tags:

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

like image 780
micnguyen Avatar asked Mar 28 '15 06:03

micnguyen


People also ask

Is it possible to scroll to the top of a UITableView?

Scrolling to the top of a UITableView can be a useful feature depending on the type of app that you are making. If you have a tableview with hundreds of cells it can be annoying scrolling to the top of the tableview once you have scrolled far down the tableview.

Why use a UITableView instead of a Tableview?

Table views are more versatile than you might think. For example, many developers make their life harder using a scroll view when a UITableView would be a better choice. Finally, architecture is crucial for table views.

How do I scroll through a Tableview in action sheet?

The first scrolling action we’ll add to our action sheet is scrolling the first row in the tableview. Note that this action is different than scrolling to the top of the tableview. If the tableview has a header then scrolling to the top would scroll to the top of the header. We’ll do that action later.

What are the cells of UITableView?

The cells of UITableView are instances of UITableViewCell or its subclasses. It is the table view that adds, removes, and arranges cells in its view hierarchy. Table views reuse cells that go out of the screen to display new elements, so that:


1 Answers

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; } 
like image 192
akiraspeirs Avatar answered Oct 20 '22 22:10

akiraspeirs