【发布时间】:2017-08-24 21:46:29
【问题描述】:
如何从 firebase 获取 child 节点的 children?现在我使用此代码获取 child,但无法从第一个和第二个 childrens 获取 Images:
var images = [String]()
var images2 = [String]()
override func viewDidLoad() {
super.viewDidLoad()
nameAddressRef = Database.database().reference(withPath: "Студии")
loadImages()
}
func loadImages() {
nameAddressRef.observe(DataEventType.value, with: { (snapshot) in
var newImage = [String]()
var newImage1 = [String]()
for child in snapshot.children {
let snap = child as! DataSnapshot
let imageSnap = snap.childSnapshot(forPath: "Images")
let dict = imageSnap.value as! [String: AnyObject]
let url1 = dict["image1"] as! String
let url2 = dict["image2"] as! String
newImage.append(url1)
newImage1.append(url2)
}
self.images = newImage
self.images2 = newImage1
})
}
}
extension ViewController: UITableViewDataSource, UITableViewDelegate {
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! CategoryRow
cell.nameLabel.text = nameAddress[indexPath.section].name
let array = [images[indexPath.section],
images2[indexPath.section]]
cell.images = array.filter({ !($0.isEmpty) })
return cell
}
}
我知道我在我的功能中遗漏了一些东西,但我无法理解我遗漏了什么。 还有我的 firebase 结构:
{
"Студии" : {
"Дубаи" : {
"1" : {
"Images" : {
"image1" : "https://firebasestorage.googleapis.com/v0/b/test2-29e4b.appspot.com/o/%D0%A1%D0%BA%D1%80%D0%B8%D0%BD%D1%88%D0%BE%D1%82%202017-08-09%2011.22.04.png?alt=media&token=e3e2b8ed-6ce3-4a87-91ae-1194496bf1f1",
"name" : "qwerty"
}
},
"2" : {
"Images" : {
"image1" : "https://firebasestorage.googleapis.com/v0/b/test2-29e4b.appspot.com/o/%D0%A1%D0%BA%D1%80%D0%B8%D0%BD%D1%88%D0%BE%D1%82%202017-08-09%2011.22.04.png?alt=media&token=e3e2b8ed-6ce3-4a87-91ae-1194496bf1f1",
"name" : "qwerty2"
}
},
"Images" : {
"image1" : "https://firebasestorage.googleapis.com/v0/b/test2-29e4b.appspot.com/o/%D0%A1%D0%BA%D1%80%D0%B8%D0%BD%D1%88%D0%BE%D1%82%202017-08-09%2011.22.04.png?alt=media&token=e3e2b8ed-6ce3-4a87-91ae-1194496bf1f1"
},
"address" : "Тутушка",
"name" : "Дубаи"
}
}
}
感谢您的帮助!
【问题讨论】:
-
你能重复一下吗:
let imageSnap = snap.childSnapshot(forPath: "Images")和1->Images?
标签: ios swift firebase firebase-realtime-database