【发布时间】:2017-10-05 01:10:29
【问题描述】:
我正在尝试使用 Firebase 设置我的 CollectionView 中的单元格数量。我尝试创建一个局部变量并将其设置为与 Firebase 变量相同的值,但是当我尝试在函数外部使用它时它不起作用。我也尝试在 ViewWillAppear 中进行设置,但没有成功。
我设置导航栏标题来查看值。当它设置在闭包中时,我得到了正确的值,当我在闭包之外(在 firebase 函数之后)编写它时,它给出的值为 0。
我正在使用 swift 3
override func viewWillAppear(_ animated: Bool) {
FIRDatabase.database().reference(withPath: "data").child("numCells").observeSingleEvent(of: .value, with: { (snapshot) in
if let snapInt = snapshot.value as? Int {
// self.navigationItem.title = String(snapInt)
self.numCells = snapInt
}
}) { (error) in
print(error.localizedDescription)
}
self.navigationItem.title = String(numCells)
}
...
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of items
return numCells
}
【问题讨论】:
-
在更新
numCells后,您是否在做self.collectionView.reloadData()? -
当我在闭包外将标签设置为 numCells 时,它给出了 0 值,所以我认为问题就在那里。我刚刚尝试过,现在它工作得很好!谢谢,您可以将其发布为答案。 @Downgoat
-
这只是有时会起作用!
-
更具体的解决方案是什么? @Jay
标签: swift firebase firebase-realtime-database uicollectionview uicollectionviewcell