[英]UICollectionView insertItemsAtIndexPaths: seems to reload collectionview or autoscrolls to top
I am using a UICollectionView
in combination with a NSFetchedResultsController
. 我使用的是UICollectionView
与组合NSFetchedResultsController
。 When I fetch new data, I throw them in a SQLite database and my frc gets a notification that it will update. 当我获取新数据时,我将它们放入SQLite数据库中,我的frc收到一条通知,即将更新。 I implement the appropriate delegate methods and insert the new data in a batch update and I don't call reloadData
afterwards. 我实现了适当的委托方法,并将新数据插入批处理更新中,此后我不调用reloadData
。 But as soon as the data is inserted, the collectionview scrolls to the top (to make the new items visible?). 但是,一旦插入数据,collectionview就会滚动到顶部(使新项目可见?)。 I would like to interfere this process and imitate the Facebook/9Gag implementation. 我想干扰这个过程,并模仿Facebook / 9Gag的实现。 When there is new data, a button is faded in which lets the user scroll to the top. 当有新数据时,淡入淡出的按钮使用户可以滚动到顶部。 At the time the button is shown, the new items are already inserted, so the user can also just scroll up himself. 在显示按钮时,新项目已经插入,因此用户也可以自己向上滚动。 I tried the following: 我尝试了以下方法:
UIScrollViewDelegate
methods to see if I could stop the collectionView from scrolling (none of the methods is called during this update process) 钩入UIScrollViewDelegate
方法以查看是否可以阻止collectionView滚动(在此更新过程中未调用任何方法) willUpdate
and just insert the items when the button is pressed. 显示“滚动显示更多”按钮上时,FRC willUpdate
和刚刚插入的项目时按下按钮时。 The problem is that the user cannot scroll up himself 问题是用户无法向上滚动自己 I experimented the same problem, by calling "insertItemAtIndexPaths" to increment data into my collectionView. 我通过调用“ insertItemAtIndexPaths”将数据递增到我的collectionView中,实验了同样的问题。
I noticed two things : - If you add items AFTER the current visible index path, there's no automatic scroll - If you add items BEFORE the current visible index path, there's no automatic scroll, like you said, but the contentOffset is kept at the same position, but with different contentSize, because you've just added new items. 我注意到两件事:-如果在当前可见索引路径之后添加项目,则不会自动滚动-如果在当前可见索引路径之前添加项目,则不会像您所说的那样自动滚动,但是contentOffset保持不变位置,但具有不同的contentSize,因为您刚刚添加了新项目。
To deal with this "side effect", I used the solution mentioned here : UICollectionView insert cells above maintaining position (like Messages.app) 为了解决这种“副作用”,我使用了这里提到的解决方案: UICollectionView在保持位置上方插入单元格(例如Messages.app)
As it's explained in the following post, the "CATransaction.setDisableAction(false)" is the key thing to update smoothly the contentOffset position. 如下面的帖子中所述,“ CATransaction.setDisableAction(false)”是平滑更新contentOffset位置的关键。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.