菜鸟教程小白 发表于 2022-12-11 19:19:12

ios - 从 Firebase 数据库 Swift 3 中删除子数据


                                            <p><p>如果可能,请帮助我。当用户使用 removeValue 方法调用 delete editingStyle 函数时,我想删除 tableView 中的数据。到目前为止,我有一个项目类:</p>

<pre><code>class Item : NSObject {

    var itemName: String!
    var itemDate: String!
    var itemID: String!

    init(itemName: String, itemDate: String, itemID: String) {

      self.itemName = itemName
      self.itemDate = itemDate
      self.itemID = itemID



    }

    init(snapshot: FIRDataSnapshot) {

      let snapshotValue = snapshot.value as? NSDictionary

      self.itemName = snapshotValue![&#34;itemName&#34;] as! String
      self.itemDate = snapshotValue![&#34;itemDate&#34;] as! String
      self.itemID = snapshotValue![&#34;itemID&#34;] as! String
    }

      func toAnyObject() -&gt; {
            return [&#34;itemName&#34;: itemName as AnyObject, &#34;itemDate&#34;: itemDate as AnyObject, &#34;itemID&#34;: itemID as AnyObject]
    }


}
</code></pre>

<p>现在我想删除用户 itemID 中的值,但我不确定从这里去哪里:</p>

<pre><code>override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {

    guard let uid = FIRAuth.auth()?.currentUser?.uid else {
      return
    }

    let item = itemArray
    let itemID = databaseRef.child(&#34;ItemID&#34;)

    databaseRef.child(&#34;users&#34;).child(uid).child(&#34;usersList&#34;).child(itemID).removeValue()
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>我认为您缺少“删除”编辑样式,并且需要对引用进行一些更改。试试这个:</p>

<pre><code>override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {

    guard let uid = FIRAuth.auth()?.currentUser?.uid else {
      return
    }

    let item = items
    let itemID = ref.child(&#34;users&#34;).child(uid).child(&#34;usersList&#34;).child(&#34;ItemID&#34;)

    if editingStyle == .delete {
      item.ref?.removeValue()
    }

}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 从 Firebase 数据库 Swift 3 中删除子数据,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/42912420/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/42912420/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 从 Firebase 数据库 Swift 3 中删除子数据