【问题标题】:How to delete tableview items after they are deleted in Firebase如何在 Firebase 中删除 tableview 项目后删除它们
【发布时间】:2026-02-03 03:45:02
【问题描述】:

我的 tableview 当前会更新我的表并在将新项目添加到我的 firebase 数据库时实时添加它们。问题是我无法实时删除。我将来自 firebase 的数据存储在本地数组中,然后将该数组加载到 tableview。

我试着稍微压缩一下我的代码。我还尝试将我的 removeDeletedItems() 函数中的 Firebase 代码放在我的 populateArrays() 函数中,并将其放在 .childAdded 侦听器之后,但没有运气实时删除数据。

override func viewDidLoad() {

        super.viewDidLoad()
        populateArrays()        

    }


    func removeDeletedItems() {

        let databaseRef = FIRDatabase.database().reference()
        databaseRef.child("Users").observe(FIRDataEventType.childRemoved, with: { (FIRDataSnapshot) in

            guard let emailToFind = FIRDataSnapshot.value as? String else { return }
            for (index, email) in self.usernames.enumerated() {
                if email == emailToFind {
                    let indexPath = IndexPath(row: index, section: 0)
                    self.usernames.remove(at: index)
                    self.tableView.deleteRows(at: [indexPath], with: .fade)
                    self.tableView.reloadData()
                }
            }
        })
    }

 func populateArrays(){

        let databaseRef = FIRDatabase.database().reference()
        databaseRef.child("Users").observe(FIRDataEventType.childAdded, with: { (FIRDataSnapshot) in

            if let data = FIRDataSnapshot.value as? NSDictionary {
                if let name = data[Constants.NAME] as? String {

                                self.usernames.append(name)
                                self.removeDeletedItems()
                                self.tableView.reloadData()
                }
            }
        })
    }

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)

        cell.textLabel?.text = usernames[indexPath.row]        
        return cell
    }

【问题讨论】:

    标签: ios uitableview tableview


    【解决方案1】:

    观察值不总是字典吗?你不应该检查姓名而不是电子邮件吗?

    不需要用于查找名称的循环。有一个方便的功能。

    databaseRef.child("Users").observe(FIRDataEventType.childRemoved, with: { snapshot in
    
                guard let data = snapshot.value as? [String:Any],
                      let nameToFind = data[Constants.NAME] as? String else { return }
                    if let index = self.usernames.index(of: nameToFind) {
                         let indexPath = IndexPath(row: index, section: 0)
                         self.usernames.remove(at: index)
                         self.tableView.deleteRows(at: [indexPath], with: .fade)
                         // don't reload the table view after calling `deleteRows`
                     }
                 }
            })
    

    【讨论】:

    • 我为 Constants.EMAIL 道歉 - 我有一个单独的 Constants swift 文件,而 Constants.EMAIL 仅表示“电子邮件”。我忘记改了。
    • 我更新了答案,您还应该检查added方法中的名称。
    • 不客气。并且请将所有出现的NSDictionary 替换为原生[String:Any] 并使用小写snapshot 而不是我的回答中的FIRDataSnapshot 类型。
    最近更新 更多