Reputation: 10159
I want to add rows to a table view without the visible cells being moved, similarly to Twitter for iPhone, Tweetbot and several others. Every method I have tried thus far accomplishes that eventually, but does funky animations in between. Here is the current solution, which moves cells around but eventually ends up staying in the same place.
- (void)controllerDidChangeContent:(NSFetchedResultsController*)controller
{
UITableViewCell *referenceCell;
if ([self.tableView.visibleCells count] > 0) {
referenceCell = [self.tableView.visibleCells lastObject];
}
CGFloat offset = referenceCell.frame.origin.y - self.tableView.contentOffset.y;
[self.tableView endUpdates];
[self.tableView setContentOffset:CGPointMake(0.0, referenceCell.frame.origin.y - offset) animated:NO];
}
Upvotes: 0
Views: 2744
Reputation: 3939
Just posted an answer with code snippets here
Keep uitableview static when inserting rows at the top
Basically:
Upvotes: 0
Reputation: 29
Try this:
-insertRowsAtIndexPaths:withRowAnimation:
on the UITableView
with UITableViewRowAnimationNone
. You can specify all index paths that you want to insert in one go. The number of inserted rows must match the number of new items.-setContentOffset:
just as beforeAvoid calling -beginUpdates
and -endUpdates
on the table view. -endUpdates
causes animations to be done for the original cells.
It seems that insertRowsAtIndexPaths alone may animate cells. You could try a workaround like this:
for (UITableViewCell *cell in [tableView visibleCells])
{
[cell.layer removeAllAnimations];
}
Upvotes: 1
Reputation: 4164
Could you just reload the data source, and immediately call
[tableview selectRowAtIndexPath: animated:NO scrollPosition:<#(UITableViewScrollPosition)#>];
You would probably need to get the current position which you could do with
[[tableview indexPathsForVisibleRows] objectAtIndex:0];
I haven't tested this out though, so I'm not 100% on it.
Upvotes: 0