0

I have the following code in one of my VC's (as part of my larger project). One of my Firebase DB references is called Sell_Request. It has three children (name, latitude and longitude), but I'm only concerned with name for right now.

I am trying to add it to a String array and am also printing it to the console. I am seeing the names being printed to the console, but I'm not getting the names printed in the individual cells.

Thanks for looking.

import UIKit import Firebase import FirebaseDatabase import MapKit import CoreLocation class RequestVC: UITableViewController, CLLocationManagerDelegate { var locationManager = CLLocationManager() var sellerUserNames = [String]() override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if segue.identifier == "backToMain" { self.navigationController?.navigationBar.isHidden = true } } override func viewDidLoad() { super.viewDidLoad() locationManager.delegate = self locationManager.desiredAccuracy = kCLLocationAccuracyBest locationManager.requestWhenInUseAuthorization() locationManager.startUpdatingLocation() } func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { if let location = manager.location?.coordinate { let databaseRef = FIRDatabase.database().reference() databaseRef.child("Sell_Request").queryOrderedByKey().observe(.childAdded, with: { (FIRDataSnapshot) in if let data = FIRDataSnapshot.value as? NSDictionary { if let name = data[Constants.NAME] as? String { self.sellerUserNames.append(name) print(name) } } }) } } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } // MARK: - Table view data source override func numberOfSections(in tableView: UITableView) -> Int { // #warning Incomplete implementation, return the number of sections return 1 } override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { // #warning Incomplete implementation, return the number of rows return sellerUserNames.count } override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) cell.textLabel?.text = sellerUserNames[indexPath.row] return cell } /* // Override to support conditional editing of the table view. override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool { // Return false if you do not want the specified item to be editable. return true } */ /* // Override to support editing the table view. override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) { if editingStyle == .delete { // Delete the row from the data source tableView.deleteRows(at: [indexPath], with: .fade) } else if editingStyle == .insert { // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view } } */ /* // Override to support rearranging the table view. override func tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to: IndexPath) { } */ /* // Override to support conditional rearranging of the table view. override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool { // Return false if you do not want the item to be re-orderable. return true } */ /* // MARK: - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation override func prepare(for segue: UIStoryboardSegue, sender: Any?) { // Get the new view controller using segue.destinationViewController. // Pass the selected object to the new view controller. } */ } 
3
  • 1
    Can you check your count is not 0.A After your print line reload table on main thread. Commented Feb 24, 2017 at 0:23
  • 1
    Hi, you need to reload your tableview. Add a reference of your tableview name it tableView, then at the end of your if let data statement add self.tableView.reloadData() Commented Feb 24, 2017 at 0:23
  • Thanks for your response...I'm still a little new to this. Did you mean creating a variable (var tableView: UITableView!) or creating an IBOutlet (@IBOutlet var tableView: UITableView!), or something else? Commented Feb 24, 2017 at 0:42

2 Answers 2

1

You just need to call tableView.reloadData() after you append the name to the sellerUserNames array, change your function to this...

 func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { if let location = manager.location?.coordinate { let databaseRef = FIRDatabase.database().reference() sellerUserNames.removeAll() databaseRef.child("Sell_Request").queryOrderedByKey().observe(.childAdded, with: { (FIRDataSnapshot) in if let data = FIRDataSnapshot.value as? NSDictionary { if let name = data[Constants.NAME] as? String { self.sellerUserNames.append(name) print(name) self.tableView.reloadData() } } }) } } 

Edit

where/when you want to call sellerUserNames.removeAll() depends on your code and the life cycle of your table view, but I have edited my code above to add it in where it is most likely appropriate.

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

2 Comments

Thanks! That seemed to do it...I seem to have another problem, though. I am getting the same names repeating themselves. I looked at some related SO questions, and it seems like calling self.sellerUserNames.removeAll() somewhere in the function should provide a quick fix to the problem, which it did...But it also only shows the first name.
I edited my answer to include a call to sellerUserNames.removeAll() which should solve that problem
0

TableView need to reload after data source has changes i.e sellerUserNames in your code. So after you append the name to sellerUserNames, you need to reload the tableview.

Add this line of code after you append the name.

self.tableView.reloadData()

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.