【发布时间】:2020-01-24 02:20:34
【问题描述】:
我是 swift 新手,正在开发我的第一个应用程序。在我的应用程序中有一个集合视图,我想在其上显示图像。这些图像是从 Firebase 数据库中检索的。
Firebase 数据库的结构如下
{
"CHILD1" : {
"URL1" : "https://Child1_url1.com",
"URL2" : "https://Child1_url2.com",
"URL3" : "https://Child1_url3.com"
}
"CHILD2" : {
"URL1" : "https://Child2_url1.com",
"URL2" : "https://Child2_url2.com",
}
"CHILD3" : {
"URL1" : "https://Child3_url1.com",
}
}
要检索我正在使用以下代码的网址
override func viewDidLoad() {
super.viewDidLoad()
checkSectionCount()
}
func checkSectionCount(){
Database.database().reference().observe(.value) { (snapShot: DataSnapshot) in
let snapDict = snapShot.value as? NSDictionary
let snapCnt = String(snapDict!.count)
print("snapCnt --> \(snapCnt)")// This prints the number of child counts correctly
}
}
func numberOfSections(in collectionView: UICollectionView) -> Int {
return snapCnt //I am unable to refer the snapCnt to set the number of section dynamically here
}
如上面的代码所示,我无法在 func checkSectionCount 之外引用 snapCnt。我也尝试将此值作为函数参数返回,但是 swift 无法识别出snapCnt
Database.database().reference(){ Code
}
不确定我在这里缺少什么。感谢是否有人可以指导我了解如何根据 Firebase 数据库中提供的详细信息动态设置集合视图的部分和行号。
【问题讨论】:
标签: ios swift firebase firebase-realtime-database closures