swift - 无法将数据从 Firestore 加载到 uitableview
问题描述
我能够查询数据并将其与我的模型相匹配,但无法在我的表格视图中显示它。除了情节提要外,我还有 3 个正在使用的文件。
这是主视图控制器:
class MealplanViewController: UIViewController {
var db: Firestore!
var mealplanArray = [Mealplan]()
@IBOutlet weak var mealplanTableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
mealplanTableView?.dataSource = self
mealplanTableView?.delegate = self
db = Firestore.firestore()
loadData()
// Do any additional setup after loading the view.
}
func loadData() {
userEmail = getUserEmail()
db.collection("Meal_Plans").getDocuments() {querySnapshot , error in
if let error = error {
print("\(error.localizedDescription)")
} else {
self.mealplanArray = querySnapshot!.documents.compactMap({Mealplan(dictionary: $0.data())})
print(self.mealplanArray)
DispatchQueue.main.async {
self.mealplanTableView?.reloadData()
}
}
}
}
func getUserEmail() -> String {
let user = Auth.auth().currentUser
if let user = user {
return user.email!
} else {
return "error"
}
}
}
// MARK: - Table view delegate
extension MealplanViewController: UITableViewDataSource, UITableViewDelegate {
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return mealplanArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
//let cell = tableView.dequeueReusableCell(withIdentifier: "MealplanTableViewCell", for: indexPath)
let mealplanRow = mealplanArray[indexPath.row]
let cell = tableView.dequeueReusableCell(withIdentifier: "MealplanTableViewCell") as! MealplanTableViewCell
cell.setMealplan(mealplan: mealplanRow)
return cell
}
}
这是我显示查询值之一的单元格:
class MealplanTableViewCell: UITableViewCell {
@IBOutlet weak var mealplanNameLabel: UILabel!
func setMealplan(mealplan: Mealplan) {
// Link the elements with the data in here
mealplanNameLabel.text = mealplan.mpName
print(mealplan.mpName)
}
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
最后,这是数据模型:
import Foundation
import Firebase
protocol MealplanSerializable {
init?(dictionary:[String:Any])
}
struct Mealplan {
var mealplanId:String
var mpName:String
]
}
}
extension Mealplan : MealplanSerializable {
init?(dictionary: [String : Any]) {
guard let
let mealplanId = dictionary["mealplanId"] as? String,
let mpName = dictionary["mpName"] as? String,
else { return nil }
self.init(mealplanId: mealplanId, mpName: mpName)
}
}
我得到的只是一个没有数据的空表视图。
解决方案
推荐阅读
- python - 我的 Dice 系数在训练的早期和波动阶段“超调”的解释是什么
- python - Web Scraping Selenium and Beautiful soup - 无法导出到 csv 文件
- python - 如何从循环中更新函数中的变量?
- paypal - Paypal // Wix // Quickbooks
- apache - Apache authtype Basic 无法在 50 台主机中的 1 台上运行
- python - Python - 循环中的多个 txt 文件的 Googletrans
- css - Angular 构建无法解析被覆盖的 Kendo 变量
- python - 哪个是matlab中python中bytearray()的等效函数?
- sql-server - 将 SQL 表脚手架到 MVC 模型后出现空引用错误
- c# - 从 .NET Core 3.1 Web Api 中的共享网络驱动器下载 PDF 文件