如何从特定的UICollectionViewCell中删除Firebase子节点-Swift

时间:2018-10-31 17:49:36

标签: ios swift firebase firebase-realtime-database uicollectionviewcell

我有一个UICollectionView which looks like this image ,并且有以下data structure in Firebase

我希望用户能够从收藏夹视图中删除单个帖子,然后从Firebase中删除。我在其他stackoverflow帖子上看到说我必须使用firebase中的.removeValue ,但不知道如何获取对随机子项的引用才能删除它 >。

如何从每个帖子(例如“ LPmNrvzu-aXsw_u-rEF”)访问autoId值,以便可以从Firebase中删除该子节点?

这是我用来从Firebase加载所有用户帖子的路径:

 @objc func observeUserPosts() {
    let uid = Auth.auth().currentUser?.uid
    let postsRef = Database.database().reference().child("posts").queryOrdered(byChild: "author/userid")
    postsRef.queryEqual(toValue: uid!).observe(.value) { (snapshot) in

    }
}

这是我要加载所有UICollectionView代码的扩展名

    //extension - UICollectionView for user's posts

extension ProfileViewController: UICollectionViewDataSource,UICollectionViewDelegate {


    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

        return postsuser.count

    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell: PostsCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "postsCell", for: indexPath) as! PostsCollectionViewCell

        cell.set(post: postsuser[indexPath.row])
        cell.deletePostButton.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
        cell.deletePostButton.tag = indexPath.row
        return cell
    }

    @objc func buttonAction(sender: UIButton) {

  Database.database().reference().child("posts").queryOrdered(byChild: "author/userid").observe(.value) { (snapshot) in
            if let posts = snapshot.value as? [String: AnyObject] {
                for (key, _) in posts {

                // NOW HOW DO I REFERENCE THE CELL THAT THE USER CLICKS TO DELETE?
                }
            }
        }

//        postsuser[sender.tag]

    }

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

        let vc = storyboard?.instantiateViewController(withIdentifier: "profileUsersSelectedPostViewController") as? ProfileUsersSelectedPostViewController
        self.navigationController?.pushViewController(vc!, animated: true)
        vc?.selectedpostsuser = postsuser[indexPath.row]

    }


}

1 个答案:

答案 0 :(得分:0)

这就是我设法解决所问问题的方法。...希望对您有所帮助:)

  func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell: PostsCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "postsCell", for: indexPath) as! PostsCollectionViewCell

    cell.set(post: postsuser[indexPath.row])
    cell.deletePostButton.addTarget(self, action: #selector(buttonAction(sender:)), for: .touchUpInside)
    cell.deletePostButton.tag = indexPath.row
    return cell
}

@objc func buttonAction(sender: UIButton) {

   ProgressHUD.show("Un momento", interaction: true)
    let uid = Auth.auth().currentUser?.uid
    Database.database().reference().child("posts").queryOrdered(byChild: "author/userid").queryEqual(toValue: uid!).observe(.value) { (snapshot) in
        if let posts = snapshot.value as? [String: AnyObject] {
            if let posts = snapshot.value as? [String: AnyObject] {
                for (key, postReference) in posts {
                    if let post = postReference as? [String: Any], let timestamp = post["timestamp"] as? TimeInterval, timestamp == self.postsuser[sender.tag].timestampDouble {

                        Database.database().reference().child("posts").child(key).removeValue(completionBlock: { (error, _) in
                            DispatchQueue.main.async {
                                ProgressHUD.showSuccess("Tu imagen ha sido borrada...")
                                self.postsuser.remove(at: sender.tag)
                                self.postsCollectionView.reloadData()
                                self.refresher.endRefreshing()
                            }
                        })
                    }
                }
        }
    }
}
}