2016-08-02 20 views
1

使用Firebase "ブランド"に属する "Product"値を取得しようとしています。たとえば、「ブランド」 - 「CATCH」のセルをクリックすると、すべての「CATCH」製品で新しいtableViewが表示されます。どうすればこれを達成できますか?これはFirebase構造である:SwiftでFirebaseを使用して値のプロパティを取得

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

そして、このように私が使用して「製品」の値を取得しようとしています:

enter image description here

ここで私はこのようなすべての「ブランド」の名前を取得しています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

を使用する必要があり、1つの配列内のすべての値とキーを取得すると思いますか?私の意見では、ブランドと製品を最初にロードしています.D –

+0

は、1人のオブザーバと1つのアレイだけを意味します。両方を取得できます –

+0

... indexpathで値を取得することによって... –

関連する問題