我正在尝试从属于其他 child 的 Firebase
获取值。例如,让我的 JSON
的这一部分:
{
"Snuses" : {
"CATCH EUCALYPTUS WHITE LARGE" : {
"Brand" : "Catch",
"Products" : "CATCH EUCALYPTUS WHITE LARGE",
"Some property" : "21.6",
},
"CATCH DRY EUCALYPTUS WHITE MINI": {
"Brand" : "Catch",
"Products" : "CATCH DRY EUCALYPTUS WHITE MINI",
"Some property" : "5.4",
}
"CRAFTED KARDUS HIGHLAND SINGLE CUT LOOSE" : {
"Brand" : "Crafted Snus",
"Products" : "CRAFTED KARDUS HIGHLAND SINGLE CUT LOOSE",
"Some property" : "32.0",
},
"Brands": {
"CATCH":0,
"CRAFTED SNUS":0
}
}
}
我已经完成了最简单的部分,将所有“品牌”查询到 tableview
中。现在我想要,如果我点击 “CATCH” 来查看其他 tableview
及其所有 “Products”。我使用 dictionary
获得了它,但是否有其他可能的方式?我想我错过了这里的逻辑。
我什至检测到点击的单元格并使用此代码完成 segue
:
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return brands.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("snusBrandsCell")
if let name = brands[indexPath.row] as? String{
let snusBrandLabel = cell?.contentView.viewWithTag(1) as! UILabel
snusBrandLabel.text = name
}
return cell!
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
print("products at \(indexPath.row) --> \(brands[indexPath.row])")
if let products = brands[indexPath.row] as? [[String:String]]{
valueTopass = products
performSegueWithIdentifier("toProducts", sender: self)
}
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?){
if (segue.identifier == "toProducts") {
var snusProductsView = segue.destinationViewController as! SnusProductsTableViewController
snusProductsView.productsValue = self.valueTopass
print(self.valueTopass)
}
}
}
最佳答案
我一定是傻了。首先我需要设置“.indexOn”:“”。以及有效的代码:
FIRDatabase.database().reference().child("Snuses").queryOrderedByChild("Brand").queryEqualToValue(brands[indexPath.row])
.observeSingleEventOfType(.ChildAdded, withBlock: { snapshot in
print(snapshot.key)
})
每次提问前都要阅读文档 :) 希望它能帮助别人。
感谢@DimaRostopira,我得到了答案
关于swift - 使用 Firebase 获取属于子项的键值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38845549/