Reputation: 1
Like the title said, I'm trying to make a UITableView
with custom cells, all the thing works properly, the array to make the cells it's okey and it contents the elements but when you show the application all the cells are void.
Here is the code of the UIViewController
:
import UIKit
import FirebaseAuth
import FirebaseDatabase
class GastosViewController: UIViewController {
let idUser = Auth.auth().currentUser?.displayName
var arrayGastos = [Gastos]()
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
var ref : DatabaseReference
ref = Database.database().reference().child("Users").child(idUser!).child("Gastos")
ref.observe(.value) { (snapshot) in
let value = snapshot.value as? NSDictionary
for j in value!{
let i = j.value as? NSDictionary
let titulo = i?["Nombre"] as? String
let descripcion = i?["Descripcion"] as? String
let precio = i?["precio"] as? Float
let fecha = i?["Fecha"] as? String
let gastoAux = Gastos(titulo: titulo!, descripcion: descripcion!, precio: precio!, date: fecha!)
print(titulo, descripcion, fecha, precio)
self.arrayGastos.append(gastoAux)
}
}
// Do any additional setup after loading the view.
}
}
extension GastosViewController : UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return arrayGastos.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let fila = arrayGastos[indexPath.row]
let celda = tableView.dequeueReusableCell(withIdentifier: "CeldaGastos") as! Celda
celda.setCelda(celda: fila)
return celda
}
}
I believe that's that code it's okey, but I'm not sure at all, in addition I will add the code of "Gastos" what is the object that I'm generating to add to the array of cells.
import Foundation
import UIKit
class Gastos {
var titulo : String
var descripcion : String
var precio : Float
var date : String
init(titulo : String, descripcion : String, precio : Float, date : String) {
self.titulo = titulo
self.descripcion = descripcion
self.precio = precio
self.date = date
}
}
And finally that's my TableViewCell
:
import UIKit
class Celda: UITableViewCell {
@IBOutlet weak var Titulo: UILabel!
@IBOutlet weak var Fecha: UILabel!
@IBOutlet weak var Precio: UILabel!
@IBOutlet weak var Descripcion: UILabel!
func setCelda(celda : Gastos){
Titulo.text = celda.titulo
Descripcion.text = celda.descripcion
Fecha.text = celda.date
Precio.text = String(celda.precio)
}
}
I'm new to Swift programation, so maybe it's an stupid error, my view controller and my cell has a custom class for them in the storyboard but the table doesn't have it, it's a table into a view controller. Please I really need help thanks
Upvotes: 0
Views: 46
Reputation: 12198
Add following didSet
block to your data source, so it refreshes the UITableView
when arrayGastos
changes
var arrayGastos = [Gastos]() {
didSet {
self.tableView.reloadData()
}
}
And instead of for-in
use following Array.compactMap
let array = value?.compactMap { j in
guard let i = j.value as? [String : Any] else { return nil }
let titulo = i["Nombre"] as? String
let descripcion = i["Descripcion"] as? String
let precio = i["precio"] as? Float
let fecha = i["Fecha"] as? String
print(titulo, descripcion, fecha, precio)
return Gastos(titulo: titulo ?? "", descripcion: descripcion ?? "", precio: precio ?? 0, date: fecha ?? "")
}
self.arrayGastos = array ?? []
Note avoid force unwrapping optionals using !
Upvotes: 0
Reputation: 100503
You need to reload as the call to firebase is asynchronous
self.arrayGastos.append(gastoAux)
}
self.tableView.reloadData()
Also think of whehter you need ref.observe(.value)
or ref.observeSingleEvent(of:.value)
Upvotes: 2