【问题标题】:Swift - show retrieved Firestore data in CollectionViewSwift - 在 CollectionView 中显示检索到的 Firestore 数据
【发布时间】:2020-04-08 09:12:33
【问题描述】:

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

【问题讨论】:

    标签: ios swift firebase google-cloud-firestore uicollectionview


    【解决方案1】:

    在回调中重新加载集合

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

    也不是有 2 个数组

    struct item {
      let title,image:String
    }
    

    【讨论】:

    • 我已经在重新加载retrieveUserDataFromDB中的数据了。
    • 您在外部而不是在回调内部重新加载,请求是异步的,因此您的重新加载行在从 firebase 检索数据之前运行,代码的执行不像您想象的那样是串行的
    • 啊啊好极了!就是这样!自己不会想出来的。谢谢大佬。
    猜你喜欢
    • 2018-11-06
    • 1970-01-01
    • 2018-11-21
    • 1970-01-01
    • 2020-04-30
    • 2020-04-26
    • 2019-02-13
    • 2021-09-20
    • 2021-11-28
    相关资源
    最近更新 更多