Swift - 在 CollectionView 中显示检索到的 Firestore 数据



我正在从我的 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
}

最新更新