简体   繁体   English

UICollectionView insertItemsAtIndexPaths:似乎重新加载collectionview或自动滚动到顶部

[英]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: 我尝试了以下方法:

  • hooking into the UIScrollViewDelegate methods to see if I could stop the collectionView from scrolling (none of the methods is called during this update process) 钩入UIScrollViewDelegate方法以查看是否可以阻止collectionView滚动(在此更新过程中未调用任何方法)
  • show the "scroll to show more"-button when the frc willUpdate and just insert the items when the button is pressed. 显示“滚动显示更多”按钮上时,FRC willUpdate和刚刚插入的项目时按下按钮时。 The problem is that the user cannot scroll up himself 问题是用户无法向上滚动自己
  • Before inserting the items, I calculate the indexPath of currently visible items AFTER the insertion (adding the number of inserted items) and scroll to that index without animation in the completion block of the batch update. 在插入项目之前,我先计算当前可见项目的indexPath,然后再进行插入(添加已插入项目的数量),并在批量更新的完成块中滚动至没有动画的索引。 This only works if I delay the scrolling, but then it flickers, because it's first scrolling up a little 这仅在我延迟滚动时才有效,但随后会闪烁,因为它先向上滚动一点

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.

相关问题 UICollectionView insertItemsAtIndexPaths:引发异常 - UICollectionView insertItemsAtIndexPaths: throws exception ios UICollectionView中的ios NSIndexPath insertItemsAtIndexPaths - ios NSIndexPath insertItemsAtIndexPaths in UICollectionView UICollectionView insertItemsAtIndexPaths引发NSInternalInconsistencyException - UICollectionView insertItemsAtIndexPaths Throws NSInternalInconsistencyException UICollectionview insertItemsAtIndexPaths 停止滚动 - UICollectionview insertItemsAtIndexPaths stops scrolling UICollectionView insertItemsAtIndexPaths:引发错误 - UICollectionView insertItemsAtIndexPaths: raising error UICollectionView重新加载数据无法更新CollectionView - UICollectionView Reload Data Not Working to update CollectionView 当标题在屏幕上时,IOS UICollectionView insertItemsAtIndexPaths崩溃 - IOS UICollectionView insertItemsAtIndexPaths crash when header is on screen InsertItemsAtIndexPaths不会在UICollectionView中更新任何内容 - InsertItemsAtIndexPaths doesn't update anything in UICollectionView 在performBatchUpdates中重新加载collectionview时未调用UICollectionview cellForItemAtIndexPath - UICollectionview cellForItemAtIndexPath not getting called when collectionview reload in performBatchUpdates 当添加项目(核心数据)时,更新UICollectionView(卡在insertItemsAtIndexPaths上) - Updating a UICollectionView when an item is added (Core Data), stuck on insertItemsAtIndexPaths
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM