L.B. L.B. - 1 year ago 175
Swift Question

Firebase Data not showing in TableView

So basically I want to get the data from Firebase and put it in the tableView and then when a cell is deleted I want to remove the data from firebase and the table view...but with my code the data is NOT even showing up in the TableView and i really don't know what's wrong...?

Here's my Database structure if it helps:

I put the data in Courses then a childByAutoId which contains CourseName,AmountOfHoles and AddedDate then get it back in a snapshot, store the snapshot in an array called courses and then get the variables for the cell in cellForRowAtIndexPath but somehow the cell is not showing on the tableView...then I would delete the cell and data in commitEditingStyle but it doesn't even get to that because the cells don't show up...

I'm new to StackOverflow, so please excuse me if something seems stupid or wrong ...dont bother to tell me tho..

class CoursesViewController: UITableViewController {

var ref = FIRDatabaseReference.init()
override func viewDidLoad() {

ref = FIRDatabase.database().reference()

tableView.allowsMultipleSelectionDuringEditing = false

//let a = ref.childByAutoId()


var courses: [FIRDataSnapshot]! = []

override func viewDidAppear(animated: Bool) {

let CoursesRef = ref.child("Courses")

CoursesRef.observeEventType(.ChildAdded, withBlock: { snapshpt in




override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.courses.count

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) ->
UITableViewCell {

let cell: UITableViewCell! = self.tableView .dequeueReusableCellWithIdentifier("ItemCell", forIndexPath: indexPath)

let courseSnap: FIRDataSnapshot! = self.courses[indexPath.row]

let course = courseSnap.value

let coursename = course?.objectForKey("CourseName") as! String
let amountofholes = course?.objectForKey("AmountOfHoles") as! String
let addeddate = course?.objectForKey("AddedDate") as! String

cell.textLabel?.text = coursename + " " + amountofholes + " Holes"
cell.detailTextLabel?.text = addeddate

return cell


override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
return true

override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == .Delete {

// Find the snapshot and remove the value
let courseitem = courses[indexPath.row]



@IBAction func addButtonDidTouch(sender: AnyObject) {

// Alert View for input

let alert = UIAlertController(title: "Course Item",message: "Add Course",preferredStyle: .Alert)

let saveAction = UIAlertAction(title: "Save", style: .Default) { (action: UIAlertAction) -> Void in

//Get Date String
let date = NSDate()

let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "dd-MM-yyyy 'at' HH:mm"
let dateString = dateFormatter.stringFromDate(date)
let courseField = alert.textFields![0]
let holesField = alert.textFields![1]

let Courses = self.ref.child("Courses").childByAutoId()

let course = ["AddedDate": dateString as AnyObject,
"CourseName": courseField.text as! AnyObject,
"AmountOfHoles": holesField.text as! AnyObject]



let cancelAction = UIAlertAction(title: "Cancel", style: .Default) { (action: UIAlertAction) -> Void in

//TextField placeholder in alert
alert.addTextFieldWithConfigurationHandler {
(courseField: UITextField!) -> Void in

courseField.placeholder = "Course Name"
alert.addTextFieldWithConfigurationHandler {
(holesField: UITextField!) -> Void in

holesField.placeholder = "Holes (6/9/18)"

//Add alert

presentViewController(alert, animated: true, completion: nil)



Answer Source

Man you have to insert your code when a snapshot is found out !

I think you can go like this :

CoursesRef.observeEventType(.ChildAdded, withBlock: { snapshot in
  self.yourTable.insertRowsAtIndexPaths([NSIndexPath(forRow: self.courses.count-1, inSection: 0)], withRowAnimation: .Bottom)