在顶部插入行时保持uitableview静态

编程入门 行业动态 更新时间:2024-10-28 06:23:15
本文介绍了在顶部插入行时保持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静态

本文发布于:2023-08-05 21:42:23,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1308226.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:静态   uitableview

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!