【问题标题】:Swift: UICollectionView pagination with FirebaseSwift:使用 Firebase 进行 UICollectionView 分页
【发布时间】:2017-01-01 16:49:07
【问题描述】:

我正在开发可能有很多帖子,一个 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 分钟前解决了这个问题,我想我解决了我自己的问题,我需要做几个测试,我会回答
  • 我很好奇你是如何解决我提到的问题的。我也仍然想知道这样的事情的最佳解决方案是什么。很高兴听到您设法解决了自己的问题!
  • 检查答案 :) 感谢 Jordi 的帮助

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


【解决方案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
如果您有任何建议或问题,请发表评论

【讨论】:

    猜你喜欢
    • 2018-04-23
    • 2019-02-11
    • 1970-01-01
    • 1970-01-01
    • 2017-11-07
    • 1970-01-01
    • 1970-01-01
    • 2020-08-25
    • 1970-01-01
    相关资源
    最近更新 更多