我遵循了1个教程,并且能够用一些数据(imageview和文本)填充collectionView:
let appleProducts = ["A", "B", "C", "D"]
let imageArray = [UIImage(named: "pug1"), UIImage(named: "pug2"), UIImage(named: "pug3"), UIImage(named: "pug4")]
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
{
return appleProducts.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath as IndexPath) as! CollectionViewCell
cell.imageView?.image = self.imageArray[indexPath.row]
cell.title?.text = self.appleProducts[indexPath.row]
return cell
}
现在从演示项目传递到我的演示,我想用从FirebaseDatabse获得的数据(图片和文本)填充此CollectionView,因此创建了以下方法:
struct item {
let pictureId: String!
let picture: String!
}
var items = [item]()
func getLatestAddedItems(){
self.items.removeAll()
let databaseRef = FIRDatabase.database().reference()
databaseRef.child("Items").observe(.value, with: {
snapshot in
//self.items.insert(item(picture: picture), at: 0)
for childSnap in snapshot.children.allObjects {
let snap = childSnap as! FIRDataSnapshot
//print(snap.key)
let picture = (snap.value as? NSDictionary)?["bookImage"] as? String ?? ""
//print(picture)
self.items.append(item(pictureId:snap.key, picture:picture))
}
print(self.items.count)
})
}
然后,我创建此按钮来调用GetLatestAddedItems方法:
@IBAction func selectAction(_ sender: AnyObject) {
getLatestAddedItems()
}
而这一检查结果:
@IBAction func gettableAction(_ sender: AnyObject) {
print(self.items[0].picture)
print(self.items[1].picture)
print(self.items[2].picture)
print(self.items.count) }
输出结果:
picture 1 link
picture 2 link
picture 3 link
3
现在,在ContentView方法中进行了必要的更改之后,一切看起来都很好并且正确无误:
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
{
return items.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath as IndexPath) as! CollectionViewCell
//cell.imageView?.image = self.imageArray[indexPath.row]
let url = NSURL(string: items[indexPath.row].picture)
let data = NSData(contentsOf: url! as URL)
cell.imageView?.image = UIImage(data: data! as Data)
cell.title?.text = self.items[indexPath.row].pictureId
return cell
}
现在,我得到一个空的ContentView,第一次使用该按钮时它会起作用,因为我调用了getLatestAddedItems()来获取数据并将其添加到Items表中,我尝试在ViewWillAppear或Viewdidload中都对其进行调用,但没有任何变化。
这就是我认为return items.count返回0的结果,因此什么都不会出现任何建议?
将您的collectionView的协议委托初始化移至ViewController生命周期范围之一,例如,viewDidLoad()
或者viewWillAppear(_animated : Bool)
如果您使用的是自定义viewController(即,将collectionView嵌入到viewController中)
并在您的用户每次从其数据库接收值时重新加载您的collectionView。
override func viewDidLoad(){
super.viewDidLoad()
self.collectionView.dataSource = self
self.collectionView.delegate = self
}
func getLatestAddedItems(){
self.items.removeAll()
let databaseRef = FIRDatabase.database().reference()
databaseRef.child("Items").observe(.childAdded, with: {
snapshot in
for childSnap in snapshot.children.allObjects {
let snap = childSnap as! FIRDataSnapshot
let picture = (snap.value as? NSDictionary)?["bookImage"] as? String ?? ""
self.items.append(item(pictureId:snap.key, picture:picture))
print(self.items.count)
self.collectionView.reloadData()
}
})
}
PS: -所有你的火力服务器调用是异步的,这需要一些时间来检索您的FBDB数据,所以就把print(self.items.count)
应该是内部completionBlock的火力点观察通话否则,如果它放在外面会前甚至称您数据已从FBDB中检索到。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句