Reputation: 2274
I am retrieving data from my Firestore database like this:
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()
}
I am calling retrieveUserDataFromDB
in viewDidLoad
and then I am trying to add it to my CollectionView
like this but it is somehow not working and I have no idea why:
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
}
It is not showing any error message but at the moment it is only showing my addItemCell
and not my ContentCells
..
Upvotes: 0
Views: 637
Reputation: 100503
Reload the collection inside the callback
for document in querySnapshot!.documents {
}
self.theCollectionView.reloadData()
Also instead of having 2 arrays do
struct item {
let title,image:String
}
Upvotes: 2