使用Firebase

时间:2016-08-09 08:22:27

标签: swift firebase firebase-realtime-database

我正在尝试从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)

        }
    }

}

1 个答案:

答案 0 :(得分:2)

我必须愚蠢。首先,我需要设置“.indexOn”:“”。并且代码有效:

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

每次在询问之前都要阅读文档:)希望它可以帮助某人。

感谢@DimaRostopira

我得到了答案