Reputation:
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:
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
}
}
Upvotes: 3
Views: 352
Reputation: 35658
I'll give you some information about this topic and then do my best to talk you out of doing it in this fashion.
First, given the following structure:
courses
course_01
holes
1: hole 1
2: hole 2
3: hole 3
name: course name 1
course_02
holes
0: hole 0
1: hole 1
2: hole 2
name: course name 2
and some Firebase code to read the data
self.myRootRef.childByAppendingPath("courses/course_01")
thisCourseRef.observeSingleEventOfType(.Value, withBlock: { snapshot in
let name = snapshot.value["name"] as! String
let holes = snapshot.value["holes"] as! NSArray
print(name)
print(holes)
})
First thing to notice is that Firebase directly supports NSArray, so you can easily read the holes directly into an array 'holes'
When the code is run for course_01, these are the results
course name 1
(
"<null>",
"hole 1",
"hole 2",
"hole 3"
)
and then when we run it for course_02...
course name 2
(
"hole 0",
"hole 1",
"hole 2"
)
Array's in Firebase are zero based so it's assigning NULL to the 0th element in the first example. So, hole #1 would really need to be the 0th element in the array in Firebase.
That being said, lets say there's a special event, National #7 day. So you want to change the name of hole #7 to something else... say 'Hole 7, the hole that cares'
With Firebase arrays, you have to read in and overwrite the entire node (the 'hole' node, lol) as you cannot access individual elements in an array.
The better option is to format your Firebase structure thusly:
holes
-Y9989jaik9
name: hole 0
hole_num: 0
-Juiijaie898
name: hole 1
hole_num: 1
.
.
.
-Ykmi9mms9889
name: Hole 7, the hole that cares
hole_num: 7
and what makes that really cool is that you can rename the holes on the fly without editing or updating the data in the other holes.
Bottom line is Firebase array's are very situational and can generally be avoided.
Hope that helps.
Upvotes: 0
Reputation: 9389
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
Upvotes: 1