我试图从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
及其所有“产品”。我用的是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/