0

So I've been trying to put some Firebase data into an array in Swift, so I can then put it into a text field. Everything is working, however, my text view is empty and contains no rows and no data. Does anyone know why this is happening? My code is below (I've imported Firebase and UIKit at the top, don't know why Stack Overflow doesn't let me put that in..sorry about that):

class SummaryEmployeesFeelingViewController: UIViewController, UITableViewDataSource{

    struct Feedback {
        let id: Int
        let feeling: String
        let date: Timestamp
    }


    var feedbackarray = [Feedback]()

    @IBOutlet weak var tableView: UITableView!

    let summarydb = Firestore.firestore()

    override func viewDidLoad() {
        super.viewDidLoad()
        addDatatoArray()
        tableView.dataSource = self
        tableView.tableFooterView = UIView()

    }

    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

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

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let feedbackcell = tableView.dequeueReusableCell(withIdentifier: "CellReuseIdentifier")!

        let text = feedbackarray[indexPath.row] as? String

        feedbackcell.textLabel?.text = text

        return feedbackcell
    }

    func addDatatoArray(){
        summarydb.collection("employeefeedback").getDocuments { (snapshot, error) in
            if error == nil && snapshot != nil{
                for document in snapshot!.documents{
                    var idValue = document.get("id")
                    var feelingValue = document.get("feeling")
                    var dateValue = document.get("date")
                    let newEntry = Feedback(id: idValue as! Int, feeling: feelingValue as! String, date: dateValue as! Timestamp)
                    self.feedbackarray.append(newEntry)

                }
        }
    }


}


}

Thank you!

1 Answer 1

1

You need to reload your tableView after getting the data

 func addDatatoArray(){
    summarydb.collection("employeefeedback").getDocuments { (snapshot, error) in
        if error == nil && snapshot != nil{
            for document in snapshot!.documents{
                var idValue = document.get("id")
                var feelingValue = document.get("feeling")
                var dateValue = document.get("date")
                let newEntry = Feedback(id: idValue as! Int, feeling: feelingValue as! String, date: dateValue as! Timestamp)
                self.feedbackarray.append(newEntry)

            }
          print("count:\(self.feedbackarray.count)")
          DispatchQueue.main.async {
            tableView.reloadData()
          }
      }
    }
}

Also set tableView delegate

override func viewDidLoad() {
        super.viewDidLoad()
        addDatatoArray()
        tableView.dataSource = self
        tableView.delegate = self
        tableView.tableFooterView = UIView()

    }

Also update your this method

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let feedbackcell = tableView.dequeueReusableCell(withIdentifier: "CellReuseIdentifier")!

        let feedBack = feedbackarray[indexPath.row] as? Feedback

        feedbackcell.textLabel?.text = feedBack.feeling

        return feedbackcell
    }
Sign up to request clarification or add additional context in comments.

21 Comments

please check print("count:(self.feedbackarray.count)") and let me know
i updated my answer ... where to get count of feedbackarray ... i want to see if it has data or not ?
you are adding newEntry in array ... newEntry have id, date, and feeling so it will print 1 ... so you should have one row of table ...
updated my answer ... update your cellForRowAt method ... each index of your array have Feedback struct ... not string ... cast it with Feedback struct
As i told you ... you have struct on each index of your feedbackarray ... so you cast it with Feedback ... now you have Feedback object ... which have id , feeling and date ...what do you want to show in cell ?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.