在顶部插入行时保持uitableview静态 [英] Keep uitableview static when inserting rows at the top

查看:124
本文介绍了在顶部插入行时保持uitableview静态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个tableView,我在顶部插入行。

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 scroll back up.

我已尝试保存基础UIScrollview的当前位置并在插入行后重置位置但结果如此在一个颤抖中,上下,虽然它最终回到了同一个地方。

I've tried saving the current position of the underlying UIScrollview and resetting the position after the rows have been inserted but this results in a judder, up and down, although it does end up back in the same place.

有没有一个很好的方法来实现这个目标?

Is there a good way of achieving this ?

更新:我使用的是beginUpdate,然后是insertRowsAtIndexPath,endUpdates。没有reloadData调用。

Update: I am using beginUpdate, then insertRowsAtIndexPath, endUpdates. There is no reloadData call.

scrollToRowAtIndexPath跳转到当前单元格的顶部(在添加行之前保存)。

scrollToRowAtIndexPath jumps to the top of the current cell (saved before adding rows).

我试过的另一种方法,最终以完全正确的速度结束,但是有一个颤抖的是。

The other approach I tried, which ends up in exactly the right pace, but with a judder is.

save tableView currentOffset. (Underlying scrollView method)
Add rows (beginUpdates,insert...,endUpdates) 
reloadData ( to force a recalulation of the scrollview size )
Recalculate the correct new offset from the bottom of the scrollview
setContentOffset (Underlying scrollview method)

麻烦是reloadData导致scrollview / tableview简单地开始滚动,然后setContentOffset将它返回到正确的位置。

Trouble is the reloadData causes the scrollview/tableview to start scrolling briefly, then the setContentOffset returns it to the correct place.

有没有办法让tableView在没有开始显示的情况下计算出它的新大小?

Is there a way of getting a tableView to work out it's new size without starting display ?

在beginAnimation中包装整个事件commitAnimation也没什么用。

Wrapping the whole thing in a beginAnimation commitAnimation doesn't help much either.

更新2:这显然可以做到 - 当您下载更新时,请查看官方Twitter应用程序。

Update 2: This can clearly be done - see the offical twitter app for one when you pull down for updates.

推荐答案

实际上没有必要总结所有行高度,
重新加载表后的新contentSize已经表示了。
所以你要做的就是计算contentSize高度的增量并将其添加到当前偏移量。

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;
    ...

这篇关于在顶部插入行时保持uitableview静态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆