Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TableViewCell Data from Firebase Database

So basically I want to get the Holes from my Database and put them in the tableView but I don't really know how. I cant figure out how I get the cells to get the title to the number of the Hole.

Here is my Database: Structure

Here is my viewcontroller:

import UIKit
import Firebase

class HolesViewController: UITableViewController {


    //FirebaseRefrences
    var ref = FIRDatabaseReference.init()

    var holes: [FIRDataSnapshot]! = []

    override func viewDidLoad() {

        //ViewDidLoad


        //Refrences to Firebase

        ref = FIRDatabase.database().reference()
        let CoursesRef = ref.child("Courses")


        //snapshot

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



            self.holes.append(snapshpt)



            self.tableView.reloadData()

        })


    }

    override func viewDidAppear(animated: Bool) {

        //ViewDidAppear
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return self.holes.count

    }

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

        let cell: UITableViewCell! = self.tableView.dequeueReusableCellWithIdentifier("HoleCell")

        let holesSnap = self.holes[indexPath.row]

        /*

         ??????

        */
        return cell
    }

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

        return true
    }


}

1 Answers

As I can see your holes array is actually an array of Course snapshots.

If you want an array with all the holes of all the courses you will need to work with the following

self.holes = snapshpt.childSnapshotForPath("Holes").children.allObjects as [FIRDataSnapshot]

If you want to get only the holes for an especific Course you should be retrieving your data like this

ref.child("Courses").child(courseId).child("Holes").observeEventType(.ChildAdded withBlock: { snapshot in
   self.holes.append(snapshot)
}

One time you have the Holes snapshots (one snapshot for each hole) in holes[indexPath.row], you just have to set the cell textLabel to the snapshot value.

cell.textLabel = holes[indexPath.row].value as! String
like image 162
adolfosrs Avatar answered May 24 '26 10:05

adolfosrs



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!