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!["itemName"] as! String
self.itemDate = snapshotValue!["itemDate"] as! String
self.itemID = snapshotValue!["itemID"] as! String
}
func toAnyObject() -> {
return ["itemName": itemName as AnyObject, "itemDate": itemDate as AnyObject, "itemID": 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("ItemID")
databaseRef.child("users").child(uid).child("usersList").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("users").child(uid).child("usersList").child("ItemID")
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]