然后,我打算在几个表格视图中显示每个孩子的值,如下所示
在表格视图中,我仅收到单个字段,而没有收到我打算显示的数据数组,也就是说,我仅收到 ShowDataOne 的值,而不收到JSON中显示的其他字段
{ "CrudPrincipal" : {
"-LeCyGwSM-Ozsf8MmADa" : {
"Clasification" : "fsdfsdf",
"Name" : "fsdfsdf",
"Uses" : "fdsfsd"
},
"-LeDVuUxNKM-IzqmWb3s" : {
"Clasification" : "fdsfnj",
"Name" : "hOdw",
"Uses" : "fjdjf"
},
"-LeMqN4MfJYl1_ef-Umy" : {
"Clasification" : "Vázquez",
"Name" : "Daniel",
"Uses" : "fdjnfjd"
},
"-LeMrCIEvwRrz8YWwbnS" : {
"Clasification" : "erf3",
"Name" : "frf3q",
"Uses" : "rf3"
},
"-LeMs5uFRP21y8KMOwPo" : {
"Clasification" : "fases",
"Name" : "dadas",
"Uses" : "gfdgdfoigj"
},
"ShowDataOne" : "Hello World, first Data!"
},
我对数据模型的代码如下:
import UIKit
import Foundation
import Firebase
import FirebaseDatabase
class oneCrudClass: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var nameFieldOne: UITextField!
@IBOutlet weak var clasificationFieldOne: UITextField!
@IBOutlet weak var usesFieldOne: UITextField!
@IBOutlet weak var tableView: UITableView!
var ref: DatabaseReference!
var postDataRef = [String]()
var databaseHandle: DatabaseHandle?
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
// Set firebase reference
ref = Database.database().reference()
// Retrieve the data and listen for changes
databaseHandle = ref.child("CrudPrincipal").observe(.childAdded, with: { (DataSnapshot) in
let crudOne = DataSnapshot.value as? String
if let actualInfo = crudOne {
self.postDataRef.append(actualInfo)
self.tableView.reloadData()
}
})
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return postDataRef.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell:UITableViewCell=UITableViewCell(style: UITableViewCell.CellStyle.subtitle, reuseIdentifier: "PostCell")
cell.textLabel?.text = postDataRef[indexPath.row]
return cell
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == UITableViewCell.EditingStyle.delete {
postDataRef.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: UITableView.RowAnimation.automatic)
}
}
@IBAction func posData(_ sender: Any) {
ref = Database.database().reference()
if nameFieldOne.text != "" && clasificationFieldOne.text != "" && usesFieldOne.text != "" {
self.ref?.child("CrudPrincipal").childByAutoId().setValue(["Name": nameFieldOne.text, "Clasification": clasificationFieldOne.text, "Uses": usesFieldOne.text])
nameFieldOne.text = ""
clasificationFieldOne.text = ""
usesFieldOne.text = ""
} else {
print("Missing fields")
}
}
}
答案 0 :(得分:0)
因为其他值不是String
let crudOne = DataSnapshot.value as? String
if let actualInfo = crudOne {
self.postDataRef.append(actualInfo)
self.tableView.reloadData()
}
更改此项以解析字典:
"-LeMs5uFRP21y8KMOwPo" : {
"Clasification" : "fases",
"Name" : "dadas",
"Uses" : "gfdgdfoigj"
}
+++添加: 在接收方,您有DataSnapshot,它可以包含任何数据类型,并且您将DataSnapshot.value用作?假定此值为字符串的String。如果没有,
self.postDataRef.append(actualInfo)和self.tableView.reloadData()
将不会执行。 该数据如下:
“-LeMs5uFRP21y8KMOwPo”:{...
不是字符串,而是字典对象。因此,您必须检查它是否为字符串->像现在这样添加,否则,必须调用
DataSnapshot.childSnapshot(forPath:“ Name”)。value as?字符串
(当然,在您的情况下)是为了让孩子们获得价值。
let crudOne = DataSnapshot.value as? String
if let actualInfo = crudOne {
self.postDataRef.append(actualInfo)
self.tableView.reloadData()
} else if let actualInfo = DataSnapshot.childSnapshot(forPath: "Name").value as? String {
self.postDataRef.append(actualInfo)
self.tableView.reloadData()
}
类似的东西