Keep uitableview static when inserting rows at the top

后端 未结 18 2089
名媛妹妹
名媛妹妹 2020-11-29 16:12

I have a tableView that I\'m inserting rows into at the top.

Whilst I\'m doing this I want the current view to stay completely still, so the rows only appear if you

18条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-29 16:33

    Everyone loves copy and pasting code examples, so here's an implementation of Andrey Z.'s answer.

    This is in my delegateDidFinishUpdating:(MyDataSourceDelegate*)delegate method

    if (self.contentOffset.y <= 0)
    {
        [self beginUpdates];
        [self insertRowsAtIndexPaths:insertedIndexPaths withRowAnimation:insertAnimation];
        [self endUpdates];
    }
    else
    {
        CGPoint newContentOffset = self.contentOffset;
        [self reloadData];
    
        for (NSIndexPath *indexPath in insertedIndexPaths)
            newContentOffset.y += [self.delegate tableView:self heightForRowAtIndexPath:indexPath];
    
        [self setContentOffset:newContentOffset];
    
        NSLog(@"New data at top of table view");
    }
    

    The NSLog at the bottom can be replaced with a call to show a view that indicated there's fresh data.

提交回复
热议问题