swift - 使用 Firebase 获取属于子项的键值

标签 swift firebase firebase-realtime-database

我正在尝试从属于其他 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/

相关文章:

ios - Swift UI 测试在 iphone4s 模拟器上失败,与其他模拟器配合良好

ios - 显示 json 序列化错误的用户友好消息

ios - 如何调用函数从 Swift 中的 UITableViewCell 类更改 viewController?

java.lang.AssertionError : impossible Caused by: java. lang.NoSuchMethodException: 值 []

java - Firebase 回收器分页适配器未在数据更改时更新

ios - 推送到 Controller 并设置 hidesBottomBarWhenPushed 后 UILabel 错位

javascript - 获取错误 Uncaught ReferenceError : firebase is not defined

Firebase事务api调用当前数据为空

javascript - 如何从 Firebase Cloud 功能中删除用户数据(任何数据)?

ios - 在后台 Swift 中发送用户的位置