代码之家  ›  专栏  ›  技术社区  ›  Chris

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

  •  0
  • Chris  · 技术社区  · 6 年前

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

    我在打电话 retrieveUserDataFromDB 在里面 viewDidLoad 然后我想把它加到我的 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 ..

    1 回复  |  直到 6 年前
        1
  •  1
  •   Shehata Gamal    6 年前

    for document in querySnapshot!.documents { 
    } 
    self.theCollectionView.reloadData()
    

    也不是有两个数组

    struct item {
      let title,image:String
    }