Swift: numberOfRowsInSection and cellForRowAt not being invoked

Dhanya Baid from

I have been trying to display data from Firebase and display in the tableView. The data is fetched properly but is not displayed. Apart from the code mentioned below, I have already added the dataSource and Delegate for the table and the identifier for the cell.

This is the HomeTableViewController

import UIKit
import FirebaseDatabase
import Firebase

class HomeTableViewController: UITableViewController {

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

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! HomeTableViewCell
    print("Here")
    let artist = artists[indexPath.row]
    cell.artist = artist
    return cell
}
let artistRef = Database.database().reference().child("Fund")
var artists = [ArtistList]()

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

    artistRef.observe(.value, with: { (snapshot) in
        self.artists.removeAll()

        for child in snapshot.children {
            let childSnapshot = child as! DataSnapshot

            let artist = ArtistList(snapshot: childSnapshot)
            print(artist)

            self.artists.insert(artist, at: 0)
        }

        self.tableView.reloadData()
    })

}
override func viewDidLoad() {
    super.viewDidLoad()
    self.navigationController?.navigationBar.isHidden = false
    self.navigationItem.hidesBackButton = true
    title = "Artcall"

    self.tableView.estimatedRowHeight = 290.0
    self.tableView.rowHeight = UITableViewAutomaticDimension
    self.tableView.reloadData()
    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = false

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem
}



}

The main code for my HomeTableViewCell is

var artist: ArtistList!{
    didSet
    {
        let dateString:String = String(format: "%@", artist.date as CVarArg)
        labelDate.text = dateString
        labelName.text = artist.name
        labelLocation.text = artist.city

    }
}

And the code for my model, ArtistList is here

class ArtistList
{
var name: String = ""
var city: String = ""
var date: Int64 = 0
let ref: DatabaseReference!

init(snapshot: DataSnapshot) {
    ref = snapshot.ref
    if let value = snapshot.value as? [String : Any] {
        name = value["artist_name"] as! String
        city = value["perf_location"] as! String
        date = value["target_date"] as! Int64
    }

}
}

ios swift uitableview

Answers

answered 6 days ago Kayla Galway #1

UI updates need to occur on the main thread if they are being called within asynchronous functions, so try putting the tableview reload on the main queue like this:

override func viewDidAppear(_ animated: Bool) {
  super.viewDidAppear(animated)

  artistRef.observe(.value, with: { (snapshot) in
    self.artists.removeAll()

    for child in snapshot.children {
        let childSnapshot = child as! DataSnapshot

        let artist = ArtistList(snapshot: childSnapshot)
        print(artist)

        self.artists.insert(artist, at: 0)
    }

    //Print the artists array to verify it is definitely populated
    print(self.artists)

    //Reload tableview on the main queue
    DispatchQueue.main.async {
      self.tableView.reloadData()
    }

  })

}