2017-11-12 7 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)를 저장하면 삭제하려는 경우 해당 키를 가져 와서 작업을 수행 할 수 있습니다.

관련 문제