2016-08-09 67 views
1

Ich versuche, Werte von Firebase zu erhalten, die anderen Kindern gehören. Zum Beispiel lässt diesen Teil meines JSON nehmen:Abrufen von Schlüsselwerten, die zu Kind mit Firebase gehören

{ 
    "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 
    } 
    } 
} 

Ich habe die einfachste Teil getan, abgefragt alle „Marken“ in tableview. Jetzt will ich das, wenn ich zum Beispiel auf "CATCH" klicken, um andere tableview mit all seinen "Produkte" zu sehen. Ich hatte es mit dictionary, aber ist es möglich, anders? Ich denke, ich vermisse die Logik hier.

Ich habe sogar die geklickt Zelle erkannt und die segue mit diesem Code gemacht:

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) 

     } 
    } 

} 

Antwort

2

ich stumm sein. Zuerst musste ich ".indexOn" einstellen: "". Und der Code, der gearbeitet:

FIRDatabase.database().reference().child("Snuses").queryOrderedByChild("Brand").queryEqualToValue(brands[indexPath.row]) 
.observeSingleEventOfType(.ChildAdded, withBlock: { snapshot in 
print(snapshot.key) 
}) 

Jedesmal Gotta docs lesen, bevor Sie fragen :) Hoffe, dass es jemand hilft.

Ich bekam die Antwort dank @DimaRostopira