我正在尝试创建用户所属聊天的tableView。我在他们的网站上关注了firebase教程,他们说可以轻松获得用户是创建孩子的一部分聊天室列表,并为该孩子添加房间名称。Swift和Firebase查询数据库
所以我的结构看起来像这样
Users
UNIQUE KEY
nickname: "name"
rooms
name: true
Room
etc etc
所以在我cellForRow我用这个代码
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
firebase.child("users").child(fUID).observeSingleEvent(of: .value, with: { snapshot in
for user in snapshot.children.allObjects as! [FIRDataSnapshot]{
self.names = (user.value?["participating"] as? String)!
}
})
cell.textLabel?.text = self.names
cell.detailTextLabel?.text = "test"
return cell
}
我得到一个错误,当我PO的名字就想出了一个空字符串
有人可以帮助我了解什么是错的,以及如何解决它?谢谢
编辑1
我得到的代码部分工作
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
let ref = firebase.child("users").child(fUID).child("participating")
ref.observeSingleEvent(of: .value, with: { snapshot in
print(snapshot.value)
var dict = [String: Bool]()
dict = snapshot.value as! Dictionary
for (key, _) in dict {
self.names = key
print(self.names)
}
self.rooms.append(self.names)
self.tableView.reloadData()
})
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.rooms.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = self.rooms[indexPath.row]
cell.detailTextLabel?.text = "test"
return cell
}
现在的问题是,有在火力2项......这是只显示一个其中
我会观察cellForRowAtIndexPath之外的整个数组。例如在viewDidAppear中。然后,一旦你有来自提取的项目重新加载tableview。 – DogCoffee
我的问题是在行self.names ....我得到一个错误EXC_BAD_Instruction .... – RubberDucky4444
您正在做的firebase的调用是在回调 - 所以你设置它的整个方式是错误的。将firebase调用移动到上述位置,而不是在该委托方法内。 – DogCoffee