【发布时间】:2021-09-21 05:40:37
【问题描述】:
您好,我在从字典的 firestore 数组创建表视图时遇到问题。
请注意,表格视图的第一个单元格是自定义单元格
对我来说,问题是因为 firestore 数组只有一个字典,正如您在此处看到的,它是数组 plantDataArray 的打印结果
print("PLANT DATA ARRAY: \(plantDataArray)")
我得到了这个
PLANT DATA ARRAY: [Pietribiasi.PlantData(plantId: "C3884CIP01", plantType: "CIP", actualStatus: "WASHING", actualRecipe: "22")]
这就是我从 firestore 获取数据并将它们放在 plantDataArray 上的方式
func loadPlantData() {
db.collection("plantsData").whereField("plantId", isEqualTo: "C3884CIP01").getDocuments() { [self]
querySnapshot, error in
if let error = error {
print("Error: \(error.localizedDescription)")
}else{
self.plantDataArray = querySnapshot!.documents.compactMap({PlantData(dictionaryPlantData: $0.data()) })
DispatchQueue.main.async {
self.detailTableView.reloadData()
}
}
}
}
使用此功能后
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return plantDataArray.count
}
然后生成表格视图单元格
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
print("INDEX ROW: \(indexPath.row)")
print("PLANT DATA ARRAY: \(plantDataArray)")
let customCell = detailTableView.dequeueReusableCell(withIdentifier: MyDetailTableViewCell.identifier, for: indexPath) as! MyDetailTableViewCell
customCell.selectionStyle = .none
let cell = detailTableView.dequeueReusableCell(withIdentifier: "DetailDefaultCell", for: indexPath)
switch indexPath.row {
case 0:
if plantDataArray[indexPath.row].plantType == "CIP"{
customCell.configure(imageName: "CIP")
} else if plantDataArray[indexPath.row].plantType == "PASTEURIZATION"{
customCell.configure(imageName: "PASTEURIZATION")
}
return customCell
case 1:
cell.textLabel?.text = "Actual Status:"
cell.detailTextLabel?.text = plantDataArray[indexPath.row].actualStatus
cell.detailTextLabel?.textColor = UIColor.gray
return cell
default:
return cell
}
}
但它只生成第一个单元格案例 0,因为 plantDataArray.count 为 1,那么我该如何解决这个问题?它接缝我必须计算字典元素而不是字典数组。还是我做错了什么?
【问题讨论】:
-
for doc in querySnapshot!.documents { }
-
对不起你的意思?
-
您是否要为
plantDataArray中的每个条目创建两个单元格?您目前在tableView(_:numberOfRowsInSection:)中为每个条目指定一行。相反,您需要return plantDataArray.count * 2。 -
我需要为 plantDataArray 中的每个条目创建一行,因此在这种情况下,将是植物类型(这个自定义单元格)的一行,植物 ID 的另一行,actualStatus 的另一行,actualRecipe 的另一行,除了第一个一个 PlantType 另一个不是自定义单元格,所以我获得了一个表格视图,其第一行与另一行不同,这是问题所在,因为我创建了第一行并返回,并且在数组中没有其他元素之后数数
标签: swift google-cloud-firestore uikit tableview