每次到达页面底部时加载接下来的 15 个帖子



我有一个功能,可以从我的Firebase数据库中获取帖子并填充集合视图,一次使用15个帖子:

ref.child("posts").queryOrderedByKey().queryLimited(toLast: 15).observeSingleEvent(of: .value, with: { (snap) in

现在,我希望每次用户到达屏幕底部时都会获取接下来的 15 个帖子。在我的cellForItem方法中,我试图检查是否已到达页面底部,如果是,请从中断的地方加载接下来的 15 个帖子。

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "postCell", for: indexPath) as! PostCell
    cell.postImage.loadImageUsingCacheWithUrlString(posts[indexPath.row].pathToImage)
    cell.postID = posts[indexPath.row].postID
    cell.postImage.contentMode = UIViewContentMode.scaleAspectFill
    if indexPath.row == posts.count - 1
    {
        fetchPosts()
    }
    return cell
}

我只是不知道如何指定当加载接下来的 15 个帖子时,它们需要从最后 15 个帖子停止的地方开始,而不仅仅是再次获取数据库中的所有帖子。

这是整个fetchPosts()函数:

func fetchPosts() {
    let ref = FIRDatabase.database().reference()
    ref.child("users").queryOrderedByKey().observe(.value, with: { snapshot in
        let users = snapshot.value as! [String : AnyObject]
        for (_, value) in users {
            if let uid = value["uid"] as? String {
                if uid == FIRAuth.auth()?.currentUser?.uid {
                    if let followingUsers = value["following"] as? [String : String] {
                        for (_, user) in followingUsers {
                            self.following.append(user)
                        }
                    }
                    self.following.append(FIRAuth.auth()!.currentUser!.uid)
                    ref.child("posts").queryOrderedByKey().queryLimited(toLast: 15).observeSingleEvent(of: .value, with: { (snap) in
                        for postSnapshot in snap.children.allObjects as! [FIRDataSnapshot] {
                            let value = postSnapshot.value as! [String : AnyObject]
                            if let userID = value["userID"] as? String {
                                for each in self.following {
                                    if each == userID {
                                        let posst = Post()
                                        if let poster = value["poster"] as? String, let likes = value["likes"] as? Int, let pathToImage = value["pathToImage"] as? String, let postID = value["postID"] as? String {
                                            posst.poster = poster
                                            posst.likes = likes
                                            posst.pathToImage = pathToImage
                                            posst.postID = postID
                                            posst.userID = userID
                                            if let people = value["peopleWhoLike"] as? [String : AnyObject] {
                                                for (_, person) in people {
                                                    posst.peopleWhoLike.append(person as! String)
                                                }
                                            }
                                            posts.append(posst)
                                        }
                                    }
                                }
                                self.collectionView.reloadData()
                            }
                        }
                    })
                    ref.removeAllObservers()
                }
            }
        }
    })
}

我可以在此处添加/更改什么以确保每次到达页面底部时按顺序加载 15 个帖子?

没有尝试过,但这是想法。我会设置两个变量:

let readLimit:Int = 15
var readOffset:Int = 15

第一个变量是硬编码的,表示限制,应在其中加载新结果。

cellForItem函数中,检查:

// Check if user scrolled to last row
if (indexPath.item + 1) == readOffset {
  // Yes, scrolled to last row
  // Increase limit to load more from database
  readOffset += readLimit
  // Call function which loads data from database
  self.fetchPosts()
} 

你看到我去哪里了吗?现在你必须稍微修改一下读取功能:更改queryLimited(toLast: 15):将 15 替换为 self.readOffset

请记住,这还没有经过测试,只是写在这里以帮助您理解。

最新更新