Dread
Dread

Reputation: 87

UITable View not populating from decoded firebase document

import UIKit
import Firebase
import FirebaseAuth

class OtherRideSearchViewController: UIViewController, UITableViewDelegate,UITableViewDataSource {

    @IBOutlet weak var SearchBar: UISearchBar!
    @IBOutlet weak var TableView: UITableView!
    let myUsers = [MyUser]()
    //var riderList = [RiderList]()

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        TableView.dataSource = self
        TableView.delegate = self
    }

    func getUsers(){
    let db = Firestore.firestore()
     db.collection("users").getDocuments() { (snapshot, err) in
         if let err = err {
             print("Error getting documents: \(err)")
         } else {
            let documents = snapshot!.documents

           try! documents.forEach{document in
            let myUser: MyUser = try document.decoded()
            print(myUser.firstName)
            }
         }
     }
  }

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

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

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "RiderListCell", for: indexPath)
        let user = myUsers[indexPath.row]
        cell.textLabel!.text = user.firstName
        cell.detailTextLabel?.text = user.lastName

        return cell
    }
}

I can't seem to get my table view to populate, I get the data from firebase fine and decode it into MyUser template but the cell just doesn't populate,

I have tried just referencing the UITableViewCell and adding text to that also without the dequeReusable cell and that didn't work either so I assume its something I'm doing wrong with the table view and cell usage.

   import Foundation

struct MyUser: Decodable{
    let firstName : String
    let lastName : String
    let email : String
    let uid : String
}

Console output from printing getUsers

Picture of the storyboard view im using

Upvotes: 0

Views: 28

Answers (2)

Putte
Putte

Reputation: 536

So I've added some steps that you can follow to accomplish this.

Make sure you always name your variables / outlets to lowercased. like "tableView", and not "TableView"

//1.) Fix your array like this
let myUsers:[MyUser] = []

override func viewDidLoad() {
    super.viewDidLoad()

    //2.) Call the getUser method
    getUsers()

    //TIP! Always start your variables / outlets with lowercased, like "tableView"
    TableView.dataSource = self
    TableView.delegate = self
}

func getUsers(){
    let db = Firestore.firestore()
    db.collection("users").getDocuments() { (snapshot, err) in
    if let err = err {
        print("Error getting documents: \(err)")
    } else {
        let documents = snapshot!.documents

        try! documents.forEach{document in
        let myUser: MyUser = try document.decoded()
            print(myUser.firstName)

            //3.)Appending the data to the array
            myUsers.append(myUser)
        }

        //4.) Reloading your tableview AFTER the foreach
        self.TableView.reloadData()
        }
    }
}

Upvotes: 1

Harish
Harish

Reputation: 2512

  1. Inside the getUsers() update the the myUsers array with the received data.
  2. Try to reload the tableView after the myUsers array is updated.

    tableView.reloadData()
    

Upvotes: 1

Related Questions