简体   繁体   English

从UITableView和Firebase删除单元格

[英]Delete cell from UITableView and Firebase

I try to delete cell (left swipe) and remove data from Firebase Database and Storage. 我尝试删除单元格(向左滑动)并从Firebase数据库和存储中删除数据。 Using this code: 使用此代码:

struct Records {

let title: String
let source: String
var length: String
let recordUrl: String
let username: String

init(dictionary: [String: Any]) {
    self.title = dictionary["title"] as? String ?? ""
    self.source = dictionary["source"] as? String ?? ""
    self.length = dictionary["length"] as? String ?? ""
    self.recordUrl = dictionary["recordUrl"] as? String ?? ""
    self.username = dictionary["username"] as? String ?? ""
}
}

I've got a var 我有一个变种

var records = [Records]()

Trying to delete cell 尝试删除单元格

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
   guard let uid = Auth.auth().currentUser?.uid else { return }
    if (editingStyle == .delete) {
        records.remove(at: indexPath.item)
        tableView.deleteRows(at: [indexPath], with: .fade)
       Database.database().reference().child("users").child(uid).child("records").removeValue()
    }
    }

Firebase JSON enter image description here Firebase JSON 在此处输入图像描述

I need to delete item inside "records"(appears as cell in my TableView), but now using my code I delete whole "records" 我需要删除“记录”中的项目(在TableView中显示为单元格),但是现在使用我的代码删除整个“记录”

You need to save the ID for the records too. 您还需要保存记录的ID。 This way you can create a path to that particular record. 这样,您可以创建该特定记录的路径。 After that just add one more child in the path. 之后,在路径中再添加一个孩子。

Like this. 像这样。

Database.database().reference().child("users").child(uid).child("records").child(record.id).removeValue()

Got some new. 有一些新东西。 Can get key of all items in branch "records" 可以获取分支“记录”中所有项目的键

guard let uid = Auth.auth().currentUser?.uid else { return }
let ref = Database.database().reference().child("users").child(uid).child("records")
ref.observeSingleEvent(of: .value, with: { (snapshot) in

guard let dictionaries = snapshot.value as? [String: Any] else { return }
let record = self.records[indexPath.item]
dictionaries.forEach({ (key, value) in
                print("Key \(key)")

but still can't get an id to delete of current item (record in branch "records") 但仍然无法获得要删除当前项目的ID(在“记录”分支中的记录)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM