[英]How to make UICollectionView not scroll when and item is added to the top
I finally got my collection view to not scroll when updated. 我终于让我的收藏夹视图在更新时不滚动。 I think it was when I added this: 我认为是在我添加以下内容时:
self.collectionView.performBatchAnimated(false, updates: {() -> Void in
self.collectionView.reloadData()
}, completion: { _ in
})
Then I realized the newest posts were going to the bottom, and I wanted them at the top. 然后我意识到最新的帖子在最底下,我希望它们在最上面。 To do this I just used added them to the array in the opposite order. 为此,我只是使用了相反的顺序将它们添加到数组中。 Now the only problem is that when a post is added and goes to the top, the collectionView moves down by one post, since a post is being added at the top. 现在唯一的问题是,当添加帖子并将其移到顶部时,collectView将向下移动一个帖子,因为在顶部添加了帖子。 This is the entire thing: 这就是整个事情:
DataService.ds.REF_POSTS.child("\(self.loggedInUser!.uid)").observeEventType(.ChildAdded, withBlock: { postDictionary in
if postDictionary.exists() {
self.posts.insert(postDictionary, atIndex: 0)
}
self.collectionView.performBatchAnimated(false, updates: {() -> Void in
self.collectionView.reloadData()
}, completion: { _ in
})
})
Since this is Firebase .ChildAdded, it runs several times on load to fill the collectionView with all the posts, then runs again every time a post is added. 由于这是Firebase .ChildAdded,因此它将在加载时运行几次,以用所有帖子填充collectionView,然后在每次添加帖子时再次运行。 How can I stop that autoscroll (by 1 screenheight)? 如何停止自动滚动(按1屏幕高度)? I tried using setContentOffset to counterscroll, but the problem is that any code I add will be performed on load for every single post. 我尝试使用setContentOffset进行反滚动,但是问题是,我添加的任何代码都将在加载时针对每个帖子执行。 So it crashed the app due to using too much memory, from scrolling up and down repeatedly like 30+ times on load. 因此,由于使用了过多的内存,导致应用程序崩溃,而导致加载和加载反复超过30次。 I just need the user to keep their scroll position when a post is added to the top of the collection view. 当帖子添加到集合视图的顶部时,我只需要用户保持其滚动位置。
Ok so this is the best I could do. 好的,这是我能做的最好的事情。 It still does a little quick screen flash, similar to that of taking a screenshot, when reloading the table. 重新加载表格时,它仍然会进行一些快速的屏幕闪烁,类似于拍摄屏幕快照。
self.posts.insert(postDictionary, atIndex: 0)
self.collectionView.performBatchAnimated(false, updates: {() -> Void in
let currentOffset = self.collectionView.contentOffset.y
let newOffset = currentOffset + self.screenHeight
self.collectionView.setContentOffset(CGPointMake(0, newOffset), animated: false)
self.collectionView.reloadDataImmediately()
}, completion: { _ in
})
This inserts an item at the top, then counter scrolls so the user keeps the position. 这会在顶部插入一个项目,然后滚动计数器,以便用户保持位置不变。 I tried probably 30+ other things to get rid of the flash, and the only way I could do it is if I somehow reload all items at different times, to kind of dodge around the user's current scroll positions. 我尝试了30多种其他方法来摆脱闪存,而唯一可行的方法是,如果我以某种方式在不同时间重新加载所有项目,以躲避用户当前的滚动位置。 If I do this, they will end up seeing some odd behavior like repeating posts and stuff, but if they go to double check it will have refreshed...so it wouldn't really be all that efficient. 如果我这样做,他们最终将看到一些奇怪的行为,例如重复发布和发布内容,但是如果他们仔细检查,它将得到刷新……因此,效率实际上并没有那么高。 If anyone knows how to get rid of the quick "flash" on reload please let me know! 如果有人知道如何摆脱重新加载时的快速“闪光”,请告诉我!
Some things I tried: disabling animation, running different things on different threads, reloading the rows one by one, reloading current row one way and the rest a different way, bunch of other stuff I don't remember lol. 我尝试过的一些事情:禁用动画,在不同的线程上运行不同的东西,一次重新加载行,一种方式重新加载当前行,其余方式以另一种方式加载,其他很多我不记得了。 If anyone can get rid of that flash that is caused with my current code then I will select their answer as correct! 如果任何人都可以摆脱由于我当前代码引起的闪烁,那么我将选择他们的答案为正确!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.