gpt4 book ai didi

ios - 从 UITableView 和 Firebase 中删除单元格

转载 作者:行者123 更新时间:2023-11-30 11:04:47 25 4
gpt4 key购买 nike

我尝试删除单元格(向左滑动)并从 Firebase 数据库和存储中删除数据。使用此代码:

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 ?? ""
}
}

我有一个变量

var records = [Records]()

尝试删除单元格

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

我需要删除“记录”内的项目(在我的 TableView 中显示为单元格),但现在使用我的代码删除整个“记录”

最佳答案

您还需要保存 ID 以供记录。这样您就可以创建该特定记录的路径。之后,只需在路径中再添加一个子级即可。

像这样。

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

关于ios - 从 UITableView 和 Firebase 中删除单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52846995/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com