将所有子值检索到表格视图单元格中的标签

我想检索“蛋糕”的所有孩子价值蛋糕价格标签。
我试图检索,但我不能安排正确的代码,也是我的入门级。

Firebase结构

在这里输入图像说明

视图控制器

在这里输入图像说明

TestTableViewCell

import UIKit class TestTableViewCell: UITableViewCell { @IBOutlet weak var cakeImage: UIImageView! @IBOutlet weak var cakeEngLabs: UILabel! @IBOutlet weak var cakeUrLabs: UILabel! @IBOutlet weak var cakeRateLabs: UILabel! } 

* TestTableViewController **

 import UIKit import Firebase class TestTableViewController: UITableViewController { @IBOutlet var cakeTableView: UITableView! var ref = FIRDatabase.database().reference() struct Cake { let cakeEngNames: String let cakeUrNames: String let cakeImages: UIImage var cakeRates: String } var cakes = [Cake(cakeEngNames: "almond Cake", cakeUrNames: "badam cake", cakeImages: UIImage(named:"Maru")!, cakeRates: ""), Cake(cakeEngNames: "Jelly Cake", cakeUrNames: "jeli ka cake", cakeImages: UIImage(named:"spices")!, cakeRates: ""), Cake(cakeEngNames: "Jelly Cake", cakeUrNames: "jeli ka cake", cakeImages: UIImage(named:"spices")!, cakeRates: ""), Cake(cakeEngNames: "Jelly Cake", cakeUrNames: "jeli ka cake", cakeImages: UIImage(named:"spices")!, cakeRates: "")] override func viewDidLoad() { super.viewDidLoad() let AlmondSnap = ref.child("Hyderabad").child("Bakery").child("Cake") AlmondSnap.observe( .value, with: { (snapshot) in let almondRate = snapshot.value as! String self.cakes[].cakeRates = almondRate print("\(almondRate)") self.tableView.reloadData() }) } override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return self.cakes.count } override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = self.tableView.dequeueReusableCell(withIdentifier: "TestTableViewCell", for: indexPath) as! TestTableViewCell cell.cakeImage.image = self.cakes[indexPath.row].cakeImages cell.cakeEngLabs.text = self.cakes[indexPath.row].cakeEngNames cell.cakeUrLabs.text = self.cakes[indexPath.row].cakeUrNames cell.cakeRateLabs.text = self.cakes[indexPath.row].cakeRates return cell } } 

所以根据评论,正如你所说的,你只需要把它和它的速率填入表中即可。 其他则是静态的(如果不是,请参考@dstepanbuild议的结构和答案)。所以,这里是如何填写你的数据在tableview中。

  var cakes = [Cake]() override func viewDidLoad() { super.viewDidLoad() let almondSnap = ref.child("Hyderabad").child("Bakery").child("Cake") almondSnap.observe( .value, with: { (snapshot) in if snapshot.hasChildren(){ for snap in snapshot.children { // snap.key produces name of cake // snap.value produces rate of cake if let node = snap as? FIRDataSnapshot, let rate = node.value as? Int { self.cakes.append(Cake(cakeEngNames: node.key, cakeUrNames: "badam cake", cakeImages: UIImage(named:"Maru")!, cakeRates: String(rate))) } } self.cakeTableView.reloadData() } }) } 

按照目前的构build,你可以访问你的Cake节点,如下所示:

 var cakes: [Cake] = [] override func viewDidLoad() { super.viewDidLoad() let AlmondSnap = ref.child("Hyderabad").child("Bakery").child("Cake") AlmondSnap.observe( .value, with: { (snapshot) in for item in snapshot.children { if let cakeFromDatabase = item as? FIRDataSnapshot { let cake = Cake() cake.cakeRates = cakeFromDatabase.value // for "Almond_Cake" this will be 800 cakes.append(cake) } } self.tableView.reloadData() }) } 

但是,根据我所看到的情况,您可能会从对当前方法进行一些devise更改中受益。

你的数据库结构可能应该是

 dgrproject-ac0e7 -- Hyderabad -- Bakery -- Cakes -- id1 -- name: Almond Cake -- rate: 800 

等等。

然后,在您的代码中,您可以执行以下操作

 var cakes: [Cake] = [] { didSet { self.tableView.reloadData() } } override func viewDidLoad() { super.viewDidLoad() let AlmondSnap = ref.child("Hyderabad").child("Bakery").child("Cake") AlmondSnap.observe( .value, with: { (snapshot) in let allCakes = [] for item in snapshot.children { if let cakeFromDatabase = item as? FIRDataSnapshot { let cake = Cake() cake.cakeEngNames = cakeFromDatabase["name"] as? String cake.cakeRates = cakeFromDatabase["rate"] as? Int allCakes.append(cake) } } cakes = allCakes }) } 

添加didSet到你的蛋糕数组,现在将自动刷新tableView,每当你设置cakes 。 这会清理代码在Firebase调用的完成处理程序中。 我们还创build了一个allCakes数组,这样我们就不allCakes一个新的Cake对象添加到cakes并且随着数据集的增长,可能会重新加载表视图数百次。

希望这可以帮助!

如果你存储在NSMutableArray的所有值使用这个

 cell.cakeRateLbl.text = “Almon Cake Rate :\(self.cakesArray[0].objectForKey(“Hyderabad”).objectAtIndex(0).objectForKey(“Bakery”).ObjectAtIndex(0).objectForKey(“Cake”).objectAtIndex(0).valueForKey(“Almon_Cake”) as! Int)\nAzna Mental Cake Rate :\(self.cakesArray[0].objectForKey(“Hyderabad”).objectAtIndex(0).objectForKey(“Bakery”).ObjectAtIndex(0).objectForKey(“Cake”).objectAtIndex(0).valueForKey(“Azna_Mental_Cake”) as! Int)\nBlack Forest Cake Rate :\(self.cakesArray[0].objectForKey(“Hyderabad”).objectAtIndex(0).objectForKey(“Bakery”).ObjectAtIndex(0).objectForKey(“Cake”).objectAtIndex(0).valueForKey(“Black_Forest_Cake”) as! Int)” 

或者你可以试试这个

 cell.cakeRateLbl.text = “Almon Cake Rate :\(self.cakes[indexpath.row].Almond_Cake)\nAzna Mental Cake Rate :\(self.cakes[indexpath.row].Azna_Mental_Cake)\nBlack Forest Cake Rate :\(self.cakes[indexpath.row].Black_Forest_Cake)”