ios - 删除带条件的字典对象 - Firebase - Swift

标签 ios swift firebase firebase-realtime-database

在我的应用程序中,我需要列出用户注册的信息,有一个“enable”字段,其中“on”不会在tableViewController中显示,如果“yes”将列出,代码如下寻求帮助。 不应显示第一项。 谢谢。

Picture of tableViewController

let snapshot = self.listaDadosCombustivel[indexPath.row]
let key = snapshot.key 

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "celulaDados", for: indexPath) as! CadastroDadosCell        

    let snapshot = self.listaDadosCombustivel[indexPath.row]
    let key = snapshot.key
    let snapshotAnterior = self.listaDadosCombustivel[indexPath.row.littleEndian]
    self.idCadCombustivelAnterior = snapshotAnterior.key

    if var dados = snapshot.value as? [String : Any]{
        if let enable = dados["enable"] as? String{
            if enable == "yes"{
                if let dataAbastecimento = dados["dataAbastecimento"] as? String{
                    if let valorTotal = dados["valorTotal"] as? String{
                        if let litrosTotal = dados["litroTotal"] as? String{
                            if let kmAtual = dados["kmAtual"] as? String{
                                if let combustivel = dados["combustivel"] as? String{
                                    if let consumo = dados["consumo"] as? String{
                                        cell.dataLabel.text = dataAbastecimento
                                        cell.valorTotalLabel.text = valorTotal
                                        cell.litrosTotalLabel.text = litrosTotal
                                        cell.combustivelLabel.text = combustivel
                                        cell.kmVeiculoLabel.text = kmAtual
                                        cell.kmLitroLabel.text = consumo
                                    }
                                }
                            }
                        }
                    }
                }
            }else{
                print(key)
            }
        }            
    }
    return cell
}

最佳答案

如果我对你的理解是正确的,你只是想删除禁用的数据,那么你可以首先让你的数据模型只包含启用的项目,因为你需要它在 func tableView(_ tableView: UITableView, numberOfRowsInSection section : Int) -> Int {} 方法,以便不显示空单元格。

所以当你加载你的数据时也许做这样的事情:

let filteredDadosCombustivelSnapshots = self.listaDadosCombustivel.filter { snapshot in
            if let dados = snapshot.value as? [String : Any], let enable = dados["enable"] as? String, enable == "yes" {
                return true
            }

            return false
        }

然后在您的代码中使用 filteredDadosCombustivelSnapshots。

另一个解决方案是只保留原始数据,但编写一个返回正确数量的启用项目的方法,您可以在 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {}/ 像:

func numberOfEnabledDados() -> Int {
    return self.listaDadosCombustivel.filter { snapshot in
                    if let dados = snapshot.value as? [String : Any], let enable = dados["enable"] as? String, enable == "yes" {
                        return true
                    }

                    return false
                }.count
    }

并使用:

 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return numberOfEnabledDados()
}

关于ios - 删除带条件的字典对象 - Firebase - Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54040995/

相关文章:

javascript - 使用 onClick 事件处理程序时不断调用自身

ios - 如何将数据从 Parse 传递到 UITableView?

ios - 在屏幕打开之前,重要的位置更改将不起作用

arrays - 如何将 "merge"两个数组放在一起? (逐项连接字符串)

variables - 数据类型后的问号(?)

ios - 如何测试 RxSwift 变量和 RxCocoa Observable 之间的 UI 绑定(bind)?

ios - 什么时候LAContext :evaluatePolicy result in LAError. AppCancel?

ios - kCFErrorDomainCFNetwork 错误 -1005 AFNetworking

ios - Swift 无法使用类型为 '(SomeType, Block (($T9) ->

ios - TableView 重复项和 firebase 搜索查询