【问题标题】:Basic Read from Firebase and load tableview with results (Swift)从 Firebase 中基本读取并使用结果加载 tableview (Swift)
【发布时间】:2018-06-07 17:50:33
【问题描述】:

我试图简单地读取我的 firebase 数据并用这些信息填充一个 tableview。出于某种原因,我似乎只能让第一个项目出现。我的 Firebase 数据如下所示:

{
"Filter by Color" : {
   "New Black" : {
      "Astrophyllite" : {
         "color" : "Black",
         "name" : "Astrophyllite",
         "properties" : "Astral Travel, Physic Protection"
      },
      "Onyx" : {
         "Properties" : "The Heals",
         "color" : "Black",
         "name" : "Onyx"
      }
   }
 }
}

我的 firebase 权限设置为读取:true,写入:false(我想要公共读取权限,但应用程序没有数据库更改。

我的 Swift 代码:

override func viewDidLoad() {
    ref.child("Filter by Color").child("New Black").observe(DataEventType.value, with: { (snapshot) in
        var crystalList: [CrystalDefinitionClass] = []
        for child in snapshot.children {
            if let snapshot = child as? DataSnapshot,
                let crystals = CrystalDefinitionClass(snapshot: snapshot) {
                self.crystalArray.append(crystals)


            }
        }

        //            self.crystalArray = crystalList
        self.tableView.reloadData()
    })

}

以下是我的表格视图的信息:

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { // #warning Incomplete implementation, return the number of rows // return self.Crystal.count print(crystalArray.count) return crystalArray.count }

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
    let crystalDB = crystalArray[indexPath.row]

    cell.textLabel?.text = crystalDB.name

    return cell
}

这会导致来自数据库的单个结果。在这里的任何帮助将不胜感激!

【问题讨论】:

  • 请在文本中包含您实际 Firebase 结构的格式化 sn-p。您可以从 Firebase 控制台->导出 JSON。
  • 哦,你的代码和你的结构不匹配;您的结构是 /Filter by Color/Black 并且您的代码正在寻找 /Filter by Color/New Black。而且,您正在重复使用变量名称 snapshot - 将闭包内的快照命名为 childSnap 之类的名称而不是相同的名称是合适的。
  • 我的 json 数据:{“按颜色过滤”:{“黑色”:{“Astrophyllite”:{“颜色”:“黑色”,“名称”:“Astrophyllite”,“属性”: "Astral Travel, Psychic Protection" }, "Black Quartz" : { "Color" : "Black", "Crystal" : "Black Quartz", "Name" : "Black Quartz" }, } } }
  • “New Black”是对一些 json 数据进行重构的尝试(我在想也许我做错了什么。现在快照重用是我没有抓住的东西。我想调查一下。我在网上做了很多研究,并试图找到不同的方法来完成这项任务,所以我可能已经重叠了一些。奇怪的是,第一个项目出现在 tableview 中,但就是这样。我尝试将其移动到视图会出现,然后在视图中运行辅助 reload() 确实加载(认为它可能只是加载一个)但没有去
  • 问题是您的第一个节点有 properties,而第二个节点有 Properties。 Firebase 对这些键区分大小写,因此它们需要保持一致。

标签: ios swift uitableview firebase


【解决方案1】:

Firebase 具有区分大小写的键,因此在这种情况下,在 New Black 节点中,第一个子节点具有“属性”

  "Astrophyllite" : {
     "color" : "Black",
     "name" : "Astrophyllite",
     "properties" : "Astral Travel, Physic Protection"
  },

第二个子节点有“属性”

  "Onyx" : {
     "Properties" : "The Heals",
     "color" : "Black",
     "name" : "Onyx"
  }

Firebase 会考虑这两个不同的键。通常,子节点键在子节点之间保持一致至关重要,这样才能正确读取和查询它们。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多