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

Posted

技术标签:

【中文标题】如何从特定的 UICollectionViewCell 中删除 firebase 子节点 - Swift【英文标题】:How to remove a firebase child node from a specific UICollectionViewCell - Swift 【发布时间】:2018-10-31 17:49:35 【问题描述】:

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

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

如何访问每个帖子的 autoId 值,例如“LPmNrvzu-aXsw_u-rEF”,以便从 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】:

这就是我设法解决我提出的问题的方法......希望它有所帮助:)

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


【讨论】:

以上是关于如何从特定的 UICollectionViewCell 中删除 firebase 子节点 - Swift的主要内容,如果未能解决你的问题,请参考以下文章

如何从分隔类型文件中的特定行打印特定字段

如何从较大列表中的特定列表中检索特定值?

如何从特定索引映射然后在特定索引之后停止[重复]

如何从phpmyadmin中的数据库中的特定表中导出特定列

如何防止从特定类调用公共方法

如何从商店获取特定字段的所有值?