斯威夫特:UICollectionView分页与火力地堡
问题描述:
我正在开发,可能有很多帖子,一个的CollectionView的职位和火力地堡作为后端
一切都是好的,按预期方式工作 但问题正在CollectionView使用分页(加载更多)
我试着做它,它有点工作,它只是跳进去,并且屏幕进入CollectionView的顶部,这是我绝对,
我会告诉你我做了什么
这是我的ViewDidAppear:斯威夫特:UICollectionView分页与火力地堡
var numOfItemsToShow = 5 //Five posts
override func viewWillAppear(animated: Bool) {
ref.child("Posts").queryOrderedByChild("count").queryLimitedToLast(numOfItemsToShow).observeEventType(FIRDataEventType.Value, withBlock: { (snapshot) in
var newPost = [Post]()
for post in snapshot.children {
let lostPost = Post(snapshot: post as! FIRDataSnapshot)
newPost.append(lostPost)
}
self.posts = newPost.reverse()
self.mainTbl.reloadData()
})
}
这里就是我在willDisplayCell做,所以它知道我在帖子末尾,消防5个帖子:
func collectionView(collectionView: UICollectionView, willDisplayCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
if indexPath.item == Int(numOfItems)-1 {
numOfItems+=3
print("update")
ref.child("Posts").queryOrderedByChild("count").queryLimitedToLast(numOfItems).observeEventType(FIRDataEventType.Value, withBlock: { (snapshot) in
var newPost = [Post]()
for post in snapshot.children {
let lostPost = Post(snapshot: post as! FIRDataSnapshot)
newPost.append(lostPost)
}
self.posts = newPost.reverse()
self.mainTbl.reloadData()
})
}
}
那么我现在在这里失踪?
这是做分页的正确方法吗?
如果可以告诉我一个例子,可以帮助提前:)
答
感谢之后再看看我的代码
我发现,每次我滚动到最后一次,我与他们取代现有的所有职位自我,增加3个员额
而不是追加3个职位只 在
willDisplayCell
self.posts = newPost.reverse()
,导致我的屏幕上移动到顶部
,所以我的方式想通只追加3个员额
也是我做了一些改动
这里是完整的代码
ViewDidAppear:
override func viewWillAppear(animated: Bool) {
customNav()
mainTbl.reloadData()
ref.child("Posts").queryOrderedByChild("count").queryLimitedToLast(numOfItems).observeEventType(FIRDataEventType.Value, withBlock: { (snapshot) in
var newPost = [Post]()
for post in snapshot.children.reverse() {// added reverse here it , it make the newest posts appear on the top of my CV
let lostPost = Post(snapshot: post as! FIRDataSnapshot)
newPost.append(lostPost)
}
self.posts = newPost //reverse removed from here
self.mainTbl.reloadData()
})
// Do any additional setup after loading the view.
}
willDisplayCell:
func collectionView(collectionView: UICollectionView, willDisplayCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
if indexPath.item == Int(numOfItems)-1 {
let preNumOfItems = numOfItems //number of existed posts in CV
numOfItems+=3
print("update")
ref.child("Posts").queryOrderedByChild("count").queryLimitedToLast(numOfItems).observeEventType(FIRDataEventType.Value, withBlock: { (snapshot) in
var newPost = [Post]()
var count = 0 //counter added
for post in snapshot.children.reverse() {
let lostPost = Post(snapshot: post as! FIRDataSnapshot)
if count >= Int(preNumOfItems){ //To append only the 3 posts that are not existed
newPost.append(lostPost)
}
count+=1
}
self.posts += newPost //append the 3 new posts to the existed posts without replacing everything
self.mainTbl.reloadData()
})
}
}
希望我解决它的好的方式,至少它的工作原理:3
评论如果您有任何建议或问题
我无法回答您的整个问题,但想警告您您将运行的某些内容,也就是说可以同时添加新帖子用户正在滚动。这会导致在重新加载底部视图后重复输入。你可能想过滤掉已经在你的数组中的任何帖子。 –
感谢兄弟,我修复1分钟前,我想我解决我自己的问题,我需要做几个测试,我会回答 –
我很好奇你是如何解决我提到的事情。我还想知道这样的最佳解决方案是什么。很高兴听到您设法解决您自己的问题! –