Keep uitableview static when inserting rows at the top

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

    had the same problem and found a solution.

        save tableView currentOffset. (Underlying scrollView method)
        //Add rows (beginUpdates,insert...,endUpdates) // don't do this!
        reloadData ( to force a recalulation of the scrollview size )
        add newly inserted row heights to contentOffset.y here, using tableView:heightForRowAtIndexPath:
        setContentOffset (Underlying scrollview method)
    

    like this:

    - (CGFloat) firstRowHeight
    {
        return [self tableView:[self tableView] heightForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
    }
    
    ...
    CGPoint offset = [[self tableView] contentOffset];
    [self tableView] reloadData];
    offset.y += [self firstRowHeight];
    if (offset.y > [[self tableView] contentSize].height) {
        offset.y = 0;
    }
    [[self tableView] setContentOffset:offset];
    ...
    

    works perfectly, without glitches.

提交回复
热议问题