【发布时间】: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