我想检索" Cake"的所有子值。蛋糕率标签。
我正在尝试检索,但我无法安排正确的代码,我也是ios的入门级。
Firebase结构
的ViewController
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
}
}
答案 0 :(得分:1)
所以根据评论,正如你所说,你只需要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()
}
})
}
答案 1 :(得分:0)
如果您将所有值存储在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)”
答案 2 :(得分:0)
按照目前的构建,您可以访问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()
})
}
但是,根据我所看到的情况,您可能会对当前方法进行一些设计更改而受益。
您的数据库结构应该是
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
时,将cakes
添加到您的cake数组中将自动刷新tableView。这会在firebase调用的完成处理程序中清除一些代码。我们还创建了一个allCakes
数组,这样我们就不会向Cake
附加一个新的cakes
对象,并且随着数据集的增长,会导致表视图重新加载数百次。
希望这有帮助!