简体   繁体   English

来自Firebase数据库的TableViewCell数据

[英]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. 所以基本上我想从我的数据库中获取Holes并将它们放在tableView中,但我真的不知道如何。 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: 这是我的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
    }


}

As I can see your holes array is actually an array of Course snapshots. 我可以看到你的holes数组实际上是一个Course快照数组。

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. 有一次你在holes[indexPath.row]有Holes快照(每个孔一个快照),你只需要将单元格textLabel设置为快照值。

cell.textLabel = holes[indexPath.row].value as! String

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 和一些Firebase代码来读取数据

    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' 首先要注意的是Firebase直接支持NSArray,因此您可以轻松地将孔直接读入数组“孔”

When the code is run for course_01, these are the results 当为course_01运行代码时,这些是结果

course name 1
(
    "<null>",
    "hole 1",
    "hole 2",
    "hole 3"
)

and then when we run it for course_02... 然后当我们为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. Firebase中的数组基于零,因此它在第一个示例中为第0个元素分配NULL。 So, hole #1 would really need to be the 0th element in the array in Firebase. 因此,#1洞真的需要成为Firebase数组中的第0个元素。

That being said, lets say there's a special event, National #7 day. 话虽这么说,让我们说有一个特殊的事件,全国#7天。 So you want to change the name of hole #7 to something else... say 'Hole 7, the hole that cares' 所以你想把#7洞的名字改成别的......说'第7洞,关心的洞'

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. 使用Firebase阵列时,您必须读入并覆盖整个节点(“洞”节点,lol),因为您无法访问阵列中的各个元素。

The better option is to format your Firebase structure thusly: 更好的选择是格式化您的Firebase结构:

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. 底线是Firebase阵列是非常情况化的,通常可以避免。

Hope that helps. 希望有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM