【问题标题】:possible to fetch data from firebase inside of a tableView Cell?可以从tableView Cell 内的firebase 获取数据吗?
【发布时间】:2020-07-11 12:18:18
【问题描述】:

我正在尝试从 firebase 获取数据并将其放置在 tableViewCell 中。非常类似于 Instagram 上的通知视图控制器。单元格包含通知的名称、图片和说明。我可以获取数据,将其打印为控制台内的快照,但如果我在 numberOfRowsInSection 内调用 userArray.count,则该部分将返回空白。相反,如果我在 numberOfRowsInSection 中返回一个诸如 2 之类的数字,我可以成功看到评论。

用图片展示

上图是当

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

       return 2
}

被调用但是当我调用时

   func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return userArray.count
}

我明白了

我用来调用此信息的代码是

class UserModal {


       var userImage: String?
       var name: String?
       var review: String?

    init(userImage: String, name: String, review: String) {

        if let fromId = dictionary["fromId"] as? String {
           self.fromId = fromId
        }

        if let categoryLabel = dictionary["jobName"] as? String {
            self.category = categoryLabel
        }

        if let jobImage = dictionary["jobImage"] as? String {
            self.jobImage = jobImage
        }

    }


}

  var userArray = [UserRecentModal]()

     func fetchRecentsNSetupViews() {

 guard let currentID = Auth.auth().currentUser?.uid else { return }
 REVIEW_JOB.child(currentID).observe(.childAdded) { (snapshot) in
 guard let dictionary = snapshot.value as? Dictionary<String, AnyObject> else { return }

 guard let fromId = dictionary["fromId"] as? String else { return }
 guard let jobName = dictionary["jobName"] as? String else { return }
 guard let jobImageUrl = dictionary["jobImage"] as? String else { return }
//             self.imageView.loadImageUsingCacheWithUrlString(jobImageUrl)
   //
//        self.nameLabel.text = fromId
//  

  self.categoryLabel.text = jobName


    Database.fetchUser(with: fromId, completion: { (user) in
        if let postId = dictionary["fromId"] as? String {

            Database.fetchPoster(with: postId, completion: {(poster) in

                let array = UserModal(jobImage: jobImageUrl, fromId: fromId, category: jobName, dictionary: dictionary)
                self.userArr.append(array)

            })

        }
    })
    }
}

【问题讨论】:

    标签: ios firebase uitableview firebase-realtime-database


    【解决方案1】:

    你什么时候打电话给fetchRecentsNSetupViews()?如果您在cellForRowAt 方法中调用它,您会遇到问题,因为在设置表格视图时,您不知道来自fetchRecentsNSetupViews() 的数据何时会填充userArray.count。因此,表视图甚至可以知道有数据及时知道它是 2 个用户。如果您在表格视图开始设置之前下载数据会更好。

    【讨论】:

    • 它被称为 override init(frame: CGRect) { super.init(frame: frame) setupViews() fetchRecentsNSetupViews() }
    • 尝试在viewDidLoad 中启动fetchRecentsNSetupViews(),以便您可以填充userArray.count。检索数据后,重新加载viewDidLoad 中的表视图以使用userArray 的计数。让我知道这是否适合您。
    • 我在 collectionView 中有一个 tableView。我只能在 tableView 中获取评论。或者至少我认为我必须这样做。
    • 您是否尝试过按照我的建议加载数据?如果它不起作用,您可以详细说明您的问题,我可以尝试提供帮助。
    • 我尝试在 ViewDidLoad 中加载。你想继续私聊吗?
    猜你喜欢
    • 2023-01-17
    • 2021-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-04
    • 2019-09-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多