如何从Firebase检索数据?

时间:2019-02-06 08:29:45

标签: swift firebase firebase-realtime-database

我是火力四射的新手。我正在创建一个“聚集玩家的出勤”应用程序。我无法从Firebase实时数据库中检索播放器的名称。

我能够创建一个新的训练日(NewAttendanceTableViewController)来收集玩家的出勤(AttendanceViewController),这意味着每个新的训练日都会有一组玩家的出勤。我无法在AttendanceViewController中的每个单元格上显示单个玩家的姓名。我可以将其保存到Firebase数据库中。

class AttendanceViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

     var attendanceListDetails : Attendance!

    @IBOutlet weak var attendingTableView: UITableView!
    @IBOutlet weak var attendingCellLabel: UILabel!
    @IBOutlet weak var plyNameTextField: UITextField!
    @IBOutlet weak var venueInformation: UILabel!
    @IBOutlet weak var timeInformation: UILabel!

    var attendingArray : [Attending] = [Attending]()

    override func viewDidLoad() {
        super.viewDidLoad()

        self.title = attendanceListDetails.title

        self.timeInformation.text = attendanceListDetails.time
        self.venueInformation.text = attendanceListDetails.venue

        retrieveAttendingAtt()
    }

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

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "attPlayCellLabel", for: indexPath)

        let attendingList = self.attendingArray[indexPath.row]
        cell.textLabel?.text = attendingList.attending

        return cell
    }


    @IBAction func attendingButtonTapped(_ sender: Any) {

        if title == nil {
            return
        }

        if plyNameTextField.text != nil
        {
        let attendingDB = Database.database().reference().child("Attending")

            let attendingDictionary = ["AttendingPlayer": plyNameTextField.text!, "User": Auth.auth().currentUser?.email, "Title": title]

            attendingDB.childByAutoId().setValue(attendingDictionary) {
                (error, reference) in

                if error != nil {
                    print(error!)
                } else {
                    print("Attending Player saved successfully")
                }
            }

            self.plyNameTextField.text = ""

        }
    }

    func retrieveAttendingAtt() {


        let attendingDB = Database.database().reference().child("Attending")

        attendingDB.observe(.childAdded) { (snapshot) in
            let snapshotValue = snapshot.value as! Dictionary<String,String>


            let attending = snapshotValue["AttendingPlayer"]!

            let attendingPlayer = Attending()
            attendingPlayer.attending = attending

        self.attendingArray.append(attendingPlayer)

            self.attendingTableView.reloadData()
        }

    }

}

我希望该名称出现在AttendanceViewController的单元格中。

0 个答案:

没有答案