Swift:UICollectionView与Firebase的分页

时间:2016-08-25 09:58:45

标签: swift firebase pagination uicollectionview firebase-realtime-database

我正在开发可能有很多帖子,帖子的 CollectionView Firebase 作为后端
一切都很好,按预期工作 但问题是使用分页(加载更多)使CollectionView 我试过这样做,它有点工作,它只是跳进去,屏幕进入CollectionView的顶部,这是我绝对的,
我会告诉你我做了什么
这是我的 ViewDidAppear:

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()
        })

    }
}

那么我在这里失踪了什么? 这是分页的正确方法吗? 如果有可能告诉我一个有助于的例子 在此先感谢:)

1 个答案:

答案 0 :(得分:1)

再看看我的代码后,我发现每次滚动到最后我都会用他们自己替换我现有的所有帖子并添加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
如果您有任何建议或疑问,请发表评论