简体   繁体   中英

Add cells to top of UITableView

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

Try this:

  1. Record the offset to the first visible cell just as before
  2. Make sure that UITableView dataSource already contains data for new items.
  3. Call -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.
  4. Call -setContentOffset: just as before

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

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.

Just posted an answer with code snippets here

Keep uitableview static when inserting rows at the top

Basically:

  • Save the scroll offset where you would have called beginUpdate:.
  • In place of calls to InsertCell you add the cell's height to the saved offset.
  • Where you would have called endUpdate:, call reloadData, then scroll by the saved offset with NO animation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM