2017-08-24 111 views
1

我怎樣才能得到兒童孩子的火力節點?現在我用這個代碼來獲得孩子,但我不能從第一和第二兒童得到圖片獲取子節點的孩子(SWIFT 3)

var images = [String]() 
    var images2 = [String]() 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     nameAddressRef = Database.database().reference(withPath: "Студии") 

     loadImages() 
    } 
    func loadImages() { 
     nameAddressRef.observe(DataEventType.value, with: { (snapshot) in 
      var newImage = [String]() 
      var newImage1 = [String]() 

      for child in snapshot.children { 
       let snap = child as! DataSnapshot 
       let imageSnap = snap.childSnapshot(forPath: "Images") 
       let dict = imageSnap.value as! [String: AnyObject] 
       let url1 = dict["image1"] as! String 
       let url2 = dict["image2"] as! String 

       newImage.append(url1) 
       newImage1.append(url2) 

      } 
      self.images = newImage 
      self.images2 = newImage1 
     }) 
    } 
} 
extension ViewController: UITableViewDataSource, UITableViewDelegate { 

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! CategoryRow 

     cell.nameLabel.text = nameAddress[indexPath.section].name 

     let array = [images[indexPath.section], 
         images2[indexPath.section]] 
     cell.images = array.filter({ !($0.isEmpty) }) 

     return cell 
    } 
} 

我知道我失蹤在我的FUNC東西,但我缺少什麼我不明白。 我的火力結構:

{ 
    "Студии" : { 
    "Дубаи" : { 
     "1" : { 
     "Images" : { 
      "image1" : "https://firebasestorage.googleapis.com/v0/b/test2-29e4b.appspot.com/o/%D0%A1%D0%BA%D1%80%D0%B8%D0%BD%D1%88%D0%BE%D1%82%202017-08-09%2011.22.04.png?alt=media&token=e3e2b8ed-6ce3-4a87-91ae-1194496bf1f1", 
      "name" : "qwerty" 
     } 
     }, 
     "2" : { 
     "Images" : { 
      "image1" : "https://firebasestorage.googleapis.com/v0/b/test2-29e4b.appspot.com/o/%D0%A1%D0%BA%D1%80%D0%B8%D0%BD%D1%88%D0%BE%D1%82%202017-08-09%2011.22.04.png?alt=media&token=e3e2b8ed-6ce3-4a87-91ae-1194496bf1f1", 
      "name" : "qwerty2" 
     } 
     }, 
     "Images" : { 
     "image1" : "https://firebasestorage.googleapis.com/v0/b/test2-29e4b.appspot.com/o/%D0%A1%D0%BA%D1%80%D0%B8%D0%BD%D1%88%D0%BE%D1%82%202017-08-09%2011.22.04.png?alt=media&token=e3e2b8ed-6ce3-4a87-91ae-1194496bf1f1" 
     }, 
     "address" : "Тутушка", 
     "name" : "Дубаи" 
    } 
    } 
} 

感謝您的幫助!

+0

您可以重複這樣的:'讓imageSnap = snap.childSnapshot(forPath: 「圖像」)'和'1' - >' Images'? – theblindprophet

回答

2

試試這個: -

func loadImages(){ 
    Database.database().reference(withPath: 
    "Студии").child("Дубаи").observe(.value, with: { (snapShot) in 
     if snapShot.exists() { 
      let array:NSArray = snapShot.children.allObjects as NSArray 

      for child in array { 
       let snap = child as! DataSnapshot 
       if snap.value is NSDictionary { 
        let data:NSDictionary = snap.value as! NSDictionary 
        if let dict = data.value(forKey: "Images") { 
         let dictImage:NSDictionary = dict as! 
     NSDictionary 
         if let image = dictImage["image1"] { 
          print(image) 
         } 
        } 
       } 

       // newImage1.append(url2) 

      } 
     } 
    }) 
} 
+0

謝謝,它的工作。但是,如果我想從幾條路徑中獲得,我該怎麼做?例如,我將會有''Дубаи','Лондон'和'Москва'。 – onpro

1

Студии下沒有Images,使循環將不匹配。

你會想在JSON聽低一個級別:

nameAddressRef = Database.database().reference(withPath: "Студии/Дубаи")