[英]Swift - show retrieved Firestore data in CollectionView
我正在從我的 Firestore 數據庫中檢索數據,如下所示:
func retrieveUserDataFromDB() -> Void {
// local mutable "WishList" var
var wList: [Wish] = [Wish]()
let db = Firestore.firestore()
let userID = Auth.auth().currentUser!.uid
db.collection("users").document(userID).collection("wishlists").order(by: "listIDX").getDocuments() { ( querySnapshot, error) in
if let error = error {
print(error.localizedDescription)
}else {
for document in querySnapshot!.documents {
let documentData = document.data()
let listName = documentData["name"]
let listImageIDX = documentData["imageIDX"]
if listImageIDX as? Int == nil {
self.wishListImagesArray.append(UIImage(named: "iconRoundedImage")!)
self.wishListTitlesArray.append(listName as! String)
}else {
self.wishListTitlesArray.append(listName as! String)
self.wishListImagesArray.append(self.images[listImageIDX as! Int])
}
// create an empty wishlist, in case this is a new user
wList = [Wish]()
self.userWishListData.append(wList)
}
}
}
// un-hide the collection view
self.theCollectionView.isHidden = false
// reload the collection view
self.theCollectionView.reloadData()
}
我在viewDidLoad
調用retrieveUserDataFromDB
,然后我試圖像這樣將它添加到我的CollectionView
但它以某種方式不起作用,我不知道為什么:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
// "Main Wishlist" is now at item Zero in the
// wish lists array, so no need to treat it differently than
// any other list
// if indexPath.item is less than data count, return a "Content" cell
if indexPath.item < wishListTitlesArray.count {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ContentCell", for: indexPath) as! ContentCell
cell.testLabel.text = wishListTitlesArray[indexPath.item]
cell.buttonView.setImage(wishListImagesArray[indexPath.item], for: .normal)
cell.customWishlistTapCallback = {
// let wishlistView appear
// track selected index
self.currentWishListIDX = indexPath.item
// update label in wishList view
self.wishlistLabel.text = self.wishListTitlesArray[indexPath.item]
// update image in wishList view
self.wishlistImage.image = self.wishListImagesArray[indexPath.item]
// update the data for in wishList table view
self.theTableView.wishList = self.userWishListData[indexPath.item]
// reload wishList table
self.theTableView.tableView.reloadData()
UIView.animate(withDuration: 0.3, delay: 0, options: .curveEaseIn, animations: {
self.wishlistView.transform = CGAffineTransform(translationX: 0, y: 0)
})
// let welcomeText disappear
UIView.animate(withDuration: 0.2, delay: 0, options: .curveEaseOut, animations: {
self.welcomeTextLabel.transform = CGAffineTransform(translationX: 0, y: 0)
})
}
return cell
}
// past the end of the data count, so return an "Add Item" cell
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "AddItemCell", for: indexPath) as! AddItemCell
// set the closure
cell.tapCallback = {
self.listNameTextfield.becomeFirstResponder()
// let newListView appear
UIView.animate(withDuration: 0.3, delay: 0, options: .curveEaseOut, animations: {
self.blurrImage.alpha = 0.96
self.blurrImage.transform = CGAffineTransform(translationX: 0, y: 0)
self.newListView.transform = CGAffineTransform(translationX: 0, y: 0)
self.view.layoutIfNeeded()
})
self.appWillEnterForegroundHandler()
}
return cell
}
它沒有顯示任何錯誤消息,但目前它只顯示我的addItemCell
而不是我的ContentCells
..
在回調中重新加載集合
for document in querySnapshot!.documents {
}
self.theCollectionView.reloadData()
也不是有 2 個數組
struct item {
let title,image:String
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.