gpt4 book ai didi

ios - 我正在尝试在表格 View 中创建一个自定义单元格,但没有显示任何内容

转载 作者:行者123 更新时间:2023-11-28 11:34:37 24 4
gpt4 key购买 nike

如标题所述,我正在尝试使用自定义单元格制作一个 UITableView,一切正常,使单元格正常运行的数组包含元素,但当您显示时该应用程序的所有单元格都是无效的。这是 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
}

}

我相信那段代码没问题,但我一点也不确定,此外,我将添加“Gastos”的代码,我生成的要添加到的对象是什么单元格数组。

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
}

}

最后是我的 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)
}

}

我是 Swift 编程的新手,所以这可能是一个愚蠢的错误,我的 View Controller 和我的单元格在 Storyboard中有一个自定义类,但表没有,它是 View Controller 中的一个表。我真的需要帮助谢谢

最佳答案

您需要重新加载,因为对 firebase 的调用是异步的

self.arrayGastos.append(gastoAux)
}
self.tableView.reloadData()

还要考虑您是否需要 ref.observe(.value)ref.observeSingleEvent(of:.value)

关于ios - 我正在尝试在表格 View 中创建一个自定义单元格,但没有显示任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55871950/

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