【问题标题】:how to delete cell and data in firebase and swift 3如何在firebase和swift 3中删除单元格和数据
【发布时间】:2017-11-12 17:37:12
【问题描述】:

到目前为止,我正在尝试从 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

    }
}

【问题讨论】:

    标签: swift firebase firebase-realtime-database tableview


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多