Keep uitableview static when inserting rows at the top

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

    @Dean's way of using an image cache is too hacky and I think it destroys the responsiveness of the UI.

    One proper way: Use a UITableView subclass and override -setContentSize: in which you can by some means calculate how much the table view is pushed down and offset that by setting contentOffset.

    This is a simplest sample code to handle the simplest situation where all insertions happen at the top of table view:

    @implementation MyTableView
    
    - (void)setContentSize:(CGSize)contentSize {
            // I don't want move the table view during its initial loading of content.
        if (!CGSizeEqualToSize(self.contentSize, CGSizeZero)) {
            if (contentSize.height > self.contentSize.height) {
                CGPoint offset = self.contentOffset;
                offset.y += (contentSize.height - self.contentSize.height);
                self.contentOffset = offset;
            }
        }
        [super setContentSize:contentSize];
    }
    
    @end
    

提交回复
热议问题