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

我想检索“蛋糕”的所有子值到蛋糕率标签.
我试图检索,但我不能正确安排代码,我也是ios的入门级别.

Firebase结构

enter image description here

视图控制器

enter image description here

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
}
}
最佳答案
所以根据评论,正如你所说,你只需要cakename及其费率填写表格.除此之外是静态的(如果不是请求@dstepan建议的结构和答案)..所以这里是你如何填写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()
            }
        })
    } 

转载注明原文:ios – 将所有子值检索到表视图单元格中的标签 - 代码日志