从Firebase读取数据并将其放入TableView中的单元格中

时间:2016-06-29 09:20:49

标签: ios swift uitableview firebase firebase-realtime-database

所以基本上我想从uialert获取大量的孔,然后将它们放入我的数据库,然后在我的其他视图控制器中接收它们,例如18个单元格,每个单元格的标题为1 - 18,但我不知道如何获得18个单元格,单元格1获得标题1,单元格2获得标题2,依此类推。

这是我的数据库:

Structure

我想在桌面视图中添加所有漏洞:

class HolesViewController: UITableViewController {

    //TODO ADD CELLS FROM FIREBASE INSIDE COURSES.CHILD 
    //G/L FROM INPUT DATA IN NEXT VIEWCONTROLLER 

    //FirebaseRefrences
    var ref = FIRDatabaseReference.init()

    var holes: [FIRDataSnapshot]! = []

    override func viewDidLoad() {

        //ViewDidLoad



        //Refrences to Firebase

        ref = FIRDatabase.database().reference()
        let CoursesRef = ref.child("Courses")
        var holes: [FIRDataSnapshot]! = []


        print()



        //snapshot

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

            //self.courses.append(snapshpt)
            //self.tableView.reloadData()
            //print(snapshpt.childSnapshotForPath("Holes"))

            self.holes.append(snapshpt.childSnapshotForPath("Holes"))
            print(self.holes)

            //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]

        let holes = holesSnap.value

        /*

         ??????

        */

        return cell
    }

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

        return true
    }


}

在这里,我从以下位置获取孔的数据:

@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()
            print(date)

            let dateFormatter = NSDateFormatter()
            dateFormatter.dateFormat = "dd-MM-yyyy 'at' HH:mm"
            let dateString = dateFormatter.stringFromDate(date)
            print(dateString)
            //
            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]

            Courses.setValue(course)

            //SET HOLES INSIDE COURSES

            let holes = Courses.child("Holes")
            //let holesChild = holes.child("")
            let eightteenholes = ["1": "1",
                                 "2": "2",
                                 "3": "3",
                                 "4": "4",
                                 "5": "5",
                                 "6": "6",
                                 "7": "7",
                                 "8": "8",
                                "9": "9",
                                "10": "10",
                                "11": "11",
                                "12": "12",
                                "13": "13",
                                "14": "14",
                                "15": "15",
                                "16": "16",
                                "17": "17",
                                "18": "18"]

            let nineholes = ["1": "1",
                                 "2": "2",
                                 "3": "3",
                                 "4": "4",
                                 "5": "5",
                                 "6": "6",
                                 "7": "7",
                                 "8": "8",
                                 "9": "9"]

            let sixholes = ["1": "1",
                            "2": "2",
                            "3": "3",
                            "4": "4",
                            "5": "5",
                            "6": "6"]

            if holesField.text == "18"{

                holes.setValue(eightteenholes)

            } else if self.AmonuOfHoles == "9"{

                holes.setValue(nineholes)

            } else if self.AmonuOfHoles == "6"{

                holes.setValue(sixholes)

            }



        }

0 个答案:

没有答案