Keep uitableview static when inserting rows at the top

后端 未结 18 2103
名媛妹妹
名媛妹妹 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-29 16:17

    Based on Andrey Z's answer, here is a live example working perfect for me...

    int numberOfRowsBeforeUpdate = [controller.tableView numberOfRowsInSection:0];
    CGPoint currentOffset = controller.tableView.contentOffset;
    
    if(numberOfRowsBeforeUpdate>0)
    {
        [controller.tableView reloadData];
        int numberOfRowsAfterUpdate = [controller.tableView numberOfRowsInSection:0];
    
        float rowHeight = [controller getTableViewCellHeight];  //custom method in my controller
        float offset = (numberOfRowsAfterUpdate-numberOfRowsBeforeUpdate)*rowHeight;
    
        if(offset>0)
        {
            currentOffset.y = currentOffset.y+offset;
            [controller.tableView setContentOffset:currentOffset];
        }
    }
    else
        [controller.tableView reloadData];
    

提交回复
热议问题