检索所有数据值Firebase Swift

时间:2019-05-09 23:54:13

标签: swift firebase firebase-realtime-database

在我的数据库中,“ deviceRooms1,deviceRooms2,...。”我有以下形式的数据。他们都在房间的桌子上。将数据添加到rooms表时,它的形式为deviceRooms1 + 1。将数据添加到此房间表时,如何提取所有deviceRooms数据?例如,在我的代码中,我可以将数据上载到devicerooms5。添加它们时,如何自动将deviceromss添加到ArrayList中?

Firebase Database

class MainRoomTableViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 
 private var myTableView: UITableView!
    var tableRowss = [String]()
 @objc func addRoom() {
        let alert = UIAlertController(title: "Oda Ekle", message: "Oda ekle", preferredStyle: .alert)

        alert.addTextField { (textField) in
            textField.placeholder = "Odanın Adını Giriniz.."
        }

        alert.addAction(UIAlertAction(title: "Oda?", style: .default, handler: { [weak alert] (_) in
            let text = alert?.textFields![0]
            self.tableRowss.append((text?.text)!)
            let ref = Database.database().reference()

            let countıtem: Int = (Int(self.tableRowss.count))
            ref.child("0").child("Rooms").child("Rooms" + String(countıtem)).setValue(text?.text)
            self.myTableView.reloadData()

        }))
        self.present(alert, animated: true, completion: nil)
    }
 @objc func fetchRooms()
        {

            let ref = Database.database().reference().child("0").child("Rooms")

            ref.observeSingleEvent(of: .value, with: { (snapshot) in
                let value: NSDictionary = snapshot.value as! NSDictionary

                // let deviceTitle: String = value["title"] as! String

                let deviceRooms1 = value["Rooms1"] as? String ?? ""
                let deviceRooms2 = value["Rooms2"] as? String ?? ""
                let deviceRooms3 = value["Rooms3"] as? String ?? ""
                let deviceRooms4 = value["Rooms4"] as? String ?? ""
                let deviceRooms5 = value["Rooms5"] as? String ?? ""
                self.tableRowss.append(deviceRooms1)
                self.tableRowss.append(deviceRooms2)
                self.tableRowss.append(deviceRooms3)
                self.tableRowss.append(deviceRooms4)
                self.tableRowss.append(deviceRooms5)

                self.myTableView.reloadData()
            })

    }
 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print("Num: \(indexPath.row)")
        print("Value: \(tableRowss[indexPath.row])")
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return tableRowss.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "MyCell", for: indexPath as IndexPath)
        cell.textLabel!.text = "\(tableRowss[indexPath.row])"
        return cell
    }

1 个答案:

答案 0 :(得分:2)

您可以尝试

 let dic = snapshot.value as! [String:String]
 self.tableRowss = Array(dic.values)
 self.myTableView.reloadData()