如何删除firebase和swift 3中的单元格和数据

时间:2017-11-12 17:37:12

标签: swift firebase firebase-realtime-database tableview

到目前为止,我正试图从firebase中删除数据而没有运气。这是我正在使用的代码,任何人都可以帮我一把。

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

    }
}

1 个答案:

答案 0 :(得分:0)

您只是删除UITableView的数据。您需要的逻辑是从UITableViewFireabase Database中删除。正如firebase docs所述,您可以将removeValuesetValue称为nil或updateChildValues

为了使删除更容易,我将保存保存数据的对象的密钥(snapshot.keys),因此当您要删除时,您只需获取该密钥并执行操作。