我想将Firebase数据库中所有用户的第一个名称提取到表格视图单元格。当我执行我的代码时,它不起作用。 Firebase具有用户元组。在这个元组里面,我存储了所有用户的id。在这些id的内部,我存储了personalInfo元组。 Firstname在这个personalInfo里面。您可以在下方图片上查看我的Firebase快照。我的代码问题在哪里?谁能帮我 ? http://prntscr.com/huvdr9
//chatInfo.swift
class ChatInfo: UITableViewController {
let cellId = "cellId"
var users = [User] ()
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Geri", style: .plain, target:self, action: #selector(handleCancel))
tableView.register(UserCell.self, forCellReuseIdentifier: cellId)
fetchUser()
}
func handleCancel() {
dismiss(animated: true, completion: nil)
}
func fetchUser() {
Database.database().reference().child("user").observe(.childAdded, with: {(snapshot) in
if let dictionary = snapshot.value as? [String: AnyObject] {
let user = User()
user.userId = dictionary["firstname"] as! String
print(user.firstname)
self.users.append(user)
DispatchQueue.main.async(execute: {
self.tableView.reloadData()
})
}
} , withCancel: nil)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return users.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// let cell = UITableViewCell(style: .subtitle, reuseIdentifier: cellId)
let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath)
let user = users[indexPath.row]
cell.detailTextLabel?.text = user.firstname
cell.imageView?.image = UIImage(named: "mail")
return cell
}
class UserCell: UITableViewCell {
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: .subtitle, reuseIdentifier: reuseIdentifier)
}
required init?(coder aDecoder: NSCoder) {
fatalError("not implemented")
}
}
}
//user.swift
class User: NSObject {
var userId : String?
var latitude : Double?
var longitude : Double?
var firstname : String?
var email : String?
}
答案 0 :(得分:0)
您需要在线为用户添加子项:
Database.database().reference().child("user").observe(.childAdded, with: {(snapshot) in
将其更改为:
Database.database().reference().child("user").child("personalInfo").observe(.childAdded, with: {(snapshot) in