Reputation: 3
In my App I need to list information registered by the user, there is an "enable" field where "on" will not be displayed in tableViewController, if "yes" will list, code below for help. First item should not be displayed. Thank you.
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
}
Upvotes: 0
Views: 55
Reputation: 81
If I understand you correctly and you just want to remove disabled data, then you can start by having your data model with only the enabled items, because you need it in func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {}
method in order not to show empty cells.
So maybe do something like this when you load your data:
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
}
And then use filteredDadosCombustivelSnapshots in you code instead.
Another solution is just to keep original data but to write a method that returns correct number of enabled items that you can use in func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {}
like:
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
}
and to use:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return numberOfEnabledDados()
}
Upvotes: 1