2016-06-29 115 views
3

所以基本上我想從我的數據庫中獲取洞,並將它們放在tableView中,但我真的不知道如何。我無法弄清楚如何讓單元格獲得洞的數量。TableViewCell來自Firebase數據庫的數據

這裏是我的數據庫: Structure

這裏是我的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

,我可以看到你的holes數組實際上是課程快照的陣列。

如果你想用的所有課程全部孔的陣列將需要具有以下

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

工作,如果你想獲得只有一個孔especific當然,你應該可以檢索數據這樣

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

有一次你在holes[indexPath.row]孔快照(一個快照,每個洞),你只需要在細胞textLabel設置快照值。

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

OMG謝謝驚人的...一切正常,但我有一個空的頂部細胞莫名其妙? – LB16

+0

@ LB16很好!我剛剛進行了更新,可能與此有關。如果不需要,請花點時間調試holes數組,以查看您是否沒有額外的快照。 – adolfosrs

+0

完美謝謝:) – LB16

0

我給你提供一些關於這個話題的信息,然後盡我所能說服你這樣做。

首先,鑑於以下結構:

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 

和一些火力地堡代碼讀取數據

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) 
    }) 

首先要注意的是,火力地堡直接支持的NSArray,這樣你就可以輕鬆地閱讀將孔直接插入陣列'洞'

當代碼運行course_01時,這些結果是

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

,然後當我們運行它course_02 ...

course name 2 
(
    "hole 0", 
    "hole 1", 
    "hole 2" 
) 

陣在火力地堡是零基礎,所以它在第一個例子中的第0個元素分配NULL。因此,#1洞真的需要成爲Firebase陣列中的第0個元素。

這就是說,讓我們說有一個特殊的事件,國家#7日。所以你想把#7洞的名字改成別的東西......說'洞7,洞關心'

有了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 

,是什麼讓真正酷的是,你可以重命名上飛孔不編輯或更新中的其他孔的數據。

底線是Firebase陣列的非常情景化,通常可以避免。

希望有所幫助。