2016-08-02 8 views
1

使用Firebase我試圖獲得屬於「品牌」的「產品」值。例如,如果我點擊「品牌」 - 「CATCH」的單元格,我想用所有「CATCH」產品展示新的tableView。我怎樣才能做到這一點?這是火力地堡結構:使用Firebase中的Swift獲取值屬性

enter image description here

在這裏,我讓所有的「品牌」的名字是這樣的:

func parseSnusBrands(){ 
    let ref = FIRDatabase.database().reference().child("Snuses").child("Brands") 

    ref.queryOrderedByKey().observeEventType(.Value, withBlock: { (snapshot) in 
     if snapshot.exists() { 
      if let all = (snapshot.value?.allKeys)! as? [String]{ 
       self.snusBrandsArray = all 
       self.snusBrandsTableView.reloadData() 
      } 
     } 
    }) 
} 

而且這樣的,我試圖讓「產品」使用的值allKeys

func parseSnusProducts() { 
    let ref = FIRDatabase.database().reference().child("Snuses").child("Brands").child("Products") 

    ref.queryOrderedByKey().observeEventType(.Value, withBlock: { (snapshot) in 
     if snapshot.exists() { 
      if let all = (snapshot.value?.allValues)! as? [String]{ 
       self.snusBrandsArray = all 
       self.snusBrandsTableView.reloadData() 
      } 
     } 
    }) 
} 

這是我如何檢測電池點擊:

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath){ 
    parseSnusProducts() 

} 

我有2個tableViews也和自定義單元格顯示「產品」

是細胞檢測嗎?如何實現這一目標?我沒有看到互聯網上谷歌搜索的任何內容,但也許你知道一些鏈接。

回答

3

我想獲得在一個陣列中的所有值和鍵,您需要使用[[String:AnyObject]]

var snusBrandsArray = [[String:AnyObject]]() 

override func viewDidLoad() { 
    super.viewDidLoad() 
    let ref = FIRDatabase.database().reference().child("Snuses").child("Brands") 

    ref.observeSingleEventOfType(.Value, withBlock: { (snapshot) in 
     if snapshot.exists() { 
      if let all = (snapshot.value?.allKeys)! as? [String]{ 
       for a in all{ 
        if let products = snapshot.value![a] as? [[String:String]]{ 
         self.snusBrandsArray.append(["key":a,"value":products]) 
        } 
       } 
       self.yourtableview.reloadData() 
      } 
     } 
    }) 
} 


func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    //cell declaration 
    print(snusBrandsArray[indexPath.row]["key"]) 
} 

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
    print("products at \(indexPath.row) --> \(snusBrandsArray[indexPath.row]["value"])") 
} 
+0

你是什麼意思與「同一時間」?在我看來,我第一次加載品牌,然後是產品:D –

+0

這意味着只有一個觀察員和一個陣列..你可以得到兩個 –

+0

你可以...通過在indexpath獲得價值... –