为什么我在Firebase中的图片没有显示在我的收藏夹视图上?

时间:2019-05-19 19:20:41

标签: ios swift firebase uicollectionview

我正在尝试从我的收藏夹视图中的Firebase的特定用户那里获取图片。我写的一切都完美无误。但是它没有显示在我的收藏视图中。我唯一能想到的是,Swift不知道从哪个用户那里获得帖子。

这是我的代码:

class PostViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {

    @IBOutlet weak var collectionview: UICollectionView!
    var subposts = [SubPost]()
    var following = [String]()

    override func viewDidLoad() {
        super.viewDidLoad()

        fetchPosts()
    }

    @objc func fetchPosts() {
        let ref = Database.database().reference()


        ref.child("subposts").queryOrderedByKey().observe(.value, with: { (snap) in
            if let postsSnap = snap.value as? [String : AnyObject] {

                for (_,subpost) in postsSnap {
                    if let userID = subpost["userID"] as? String {
                        let posst = SubPost()
                        if let pathToImage = subpost["pathToImage"] as? String, let postID = subpost["postID"] as? String {
                            posst.pathToImage = pathToImage
                            posst.postID = postID
                            posst.userID = userID
                            self.subposts.append(posst)
                        }
                        self.collectionview.reloadData()
                    }
                }
            }
        })
        ref.removeAllObservers()
    }

    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.subposts.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "subPostCell", for: indexPath) as! SubPostCell
        cell.subPostImage.downloadImage(from: self.subposts[indexPath.row].pathToImage)
        cell.postID = self.subposts[indexPath.row].postID

        return cell
    }
}

0 个答案:

没有答案