2017-11-13 7 views
0

マークキーを取得してからUITableView swift 3から削除するにはどうすればよいですか?SwiftのFirebaseから特定のキーを削除する方法3

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

     if editingStyle == .delete { 

      self.grocery.remove(at: indexPath.row) 
      self.tableView.deleteRows(at: [indexPath], with: .automatic) 
     } 
    } 

Please refer this image.

+0

を、私はあなたが必要とするすべてはあなたのテーブルを更新することだと思いますが、ソース配列から項目を削除した後、 – Almazini

答えて

0

あなたは、単にfirebaseからそれを削除するにはnilとして値を設定することができます削除します。 remove関数を呼び出した後、tableViewから行を削除してください。

func removeObjectFromFireBase(userKey: String, removeThisGrocery: String) { 
    let ref = Database.database().reference() 
    let groceryRef = ref.child("Users").child(userKey).child("Grocery") 
    groceryRef.child(removeThisObject).setValue(nil) 
} 

func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? { 
    let deleteAction = UITableViewRowAction(style: .destructive, title: "DELETE") { (rowAction, indexPath) in 

     //Call the removeFromFirebase function with the required parameters 
     removeObjectFromFirebase(userKey: userKey, removeThisGrocery: groceryKey) 
     tableView.deleteRows(at: [indexPath], with: .automatic) 
    } 

    deleteAction.backgroundColor = UIColor.red 
    return [deleteAction, addAction] 
} 
0

次のようFirebaseから任意のデータを削除することができます

let ref = Database.database().reference() 
let groceryRef = ref.child("Users").child(userKey).child("Grocery") 
groceryRef.removeValue() 
関連する問題