Как я могу получить данные из firebase? - PullRequest
0 голосов
/ 06 февраля 2019

Я новичок в FireBase и Swift.Я создаю приложение "собрать посещаемость игрока".Я не могу получить имя игрока из базы данных 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.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...