我遵循了 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 中调用它但没有任何变化.
这就是我认为返回的 items.count 返回 0,所以没有任何建议会出现?
最佳答案
将您的 collectionView 的协议(protocol)委托(delegate)初始化移动到 ViewController 生命周期范围之一,例如 viewDidLoad()
或viewWillAppear(_animated : Bool)
如果您使用自定义 viewController(即在 viewController 中嵌入一个 collectionView)
并在每次用户从其数据库接收到值时重新加载您的 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:- 所有对你的 firebase 服务器的调用都是异步的,这需要一些时间从你的 FBDB 中检索数据,所以把 print(self .items.count)
应该在 firebase 观察调用的 completionBlock 内,否则如果它被放在外面,它甚至会在您的数据从 FBDB 检索之前被调用。
关于swift - 清空 Collection View Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40334017/