2017-11-12 16 views
0

firebaseからデータを削除しようとしています。これは私が使用しているコードです、誰でも私にそれを手にしてください。firebaseとswiftでセルとデータを削除する方法3

class TableViewController: UITableViewController { 

    var ref: FIRDatabaseReference? 

    var grocery = [Grocery]() 

    override func viewDidLoad() { 
     super.viewDidLoad() 

       loadData() 

    } 

    func loadData() { 
     let uid = FIRAuth.auth()?.currentUser?.uid 
     FIRDatabase.database().reference().child("Users").child(uid!).child("Grocery").observe(.childAdded) { (snspshot: FIRDataSnapshot) in 
      if let dict = snspshot.value as? [String: Any] { 
       let Items = dict["Item"] as! String 
       let Quintities = dict["Quintities"] as! String 
       let Done = dict["Done"] as! Bool 
       let themBe = Grocery(Items: Items, Quintitiess: Quintities, Dones: Done) 
       self.grocery.append(themBe) 
       print(themBe) 
       self.tableView.reloadData() 
      } 

     } 
    } 

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return grocery.count 
    } 

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "TasksTableViewCell") as! TasksTableViewCell 
     cell.titleLabel?.text = grocery[indexPath.row].Item 
     cell.numLabel?.text = grocery[indexPath.row].Quintities 
     return cell 
    } 

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


---------- 


import Foundation 

class Grocery { 
    var Item: String 
    var Quintities: String 
    var Done: Bool 

    init(Items: String, Quintitiess: String, Dones: Bool) { 

     Item = Items 
     Quintities = Quintitiess 
     Done = Dones 

    } 
} 

答えて

0

UITableViewのデータのみを削除しています。必要なロジックは、UITableViewFireabase Databaseから削除することです。 firebase docsには、removeValue、またはsetValueをnilに、またはupdateChildValuesに電話することができます。

削除を簡単にするために、データが保存されているオブジェクトのキー(snapshot.keys)を保存します。削除する場合は、そのキーを取得して操作を実行するだけです。

関連する問題