0

I'm learning to develop iOS app and on my first app, I have a tableView that should display the data from FirebaseDatabase.

Below is the database structure:

enter image description here

Below is the code from viewcontroler.swift

import UIKit
import Firebase

class ctrTableViewController: UITableViewController {

    var _ctrsArray = [String]()

    var dbref = Database.database().reference()

    let deviceID = UIDevice.init().identifierForVendor

    func loadCtrs() {
        // Load ctrs from DB to Tableview

        dbref.child("\(String(describing: deviceID))/ctrs").observeSingleEvent(of: .value, with: { snapshot in
            if let ctrs = snapshot.value as? [String: Any] {
                for (ctr, _) in ctrs {
                    self._ctrsArray.append(ctr)
                    self.tableView.reloadData()
                }
            }
        })
    }

    override func viewDidLoad() {

        super.viewDidLoad()
        loadCtrs()

         self.navigationItem.leftBarButtonItem = self.editButtonItem
    }

    // MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 0
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return _ctrsArray.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "ctrCell")
        cell?.textLabel?.text = _ctrsArray[indexPath.row]
        return cell!
    }

}

What am I missing? TIA for all your help.

2 Answers 2

1

You set numberOfSections to 0, so there can't be displayed any data. If you want only 1 Section set it to 1, or remove the method. It is set to 1 by default.

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

2 Comments

Thanks for your help Chris. Making the section 1 or removing the method causes the app to crash now. The error is "Fatal error: Unexpectedly found nil while unwrapping an Optional value". It gets generated at the line "return cell!"
Looks like cell is nil, and you are force unwrapping it using !, causing the error. It's better practice to first check if the cell is not nil.
0

To prevent crashing you have to register you cell for reuse, for example in viewDidLoad (before loadCtrs) in case you're using nibs add:

tableView.register(UINib(nibName: <#nibName#>, bundle: nil), forCellReuseIdentifier: "ctrCell")

And in case you're not using nibs for your custom cells:

tableView.register(<#CustomClass#>.self, forCellReuseIdentifier: "ctrCell")

2 Comments

Thanks for your tip Juri!! that resolved the issue and now data is loading in tableView. The next hurdle for me is it is displaying only the keys for each counter. How do I get the name of the counter (i.e. value of ctrName)?
If you have other questions then it would be better to ask them in a separate question.

Your Answer

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