Keep uitableview static when inserting rows at the top

后端 未结 18 2105
名媛妹妹
名媛妹妹 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:21

    There's really no need to sum up all rows height, the new contentSize after reloading the table is already representing that. So all you have to do is calculate the delta of contentSize height and add it to the current offset.

        ...
        CGSize beforeContentSize = self.tableView.contentSize;
        [self.tableView reloadData];
        CGSize afterContentSize = self.tableView.contentSize;
    
        CGPoint afterContentOffset = self.tableView.contentOffset;
        CGPoint newContentOffset = CGPointMake(afterContentOffset.x, afterContentOffset.y + afterContentSize.height - beforeContentSize.height);
        self.tableView.contentOffset = newContentOffset;
        ...
    

提交回复
热议问题