我无法从我的快照中获取数据以加载到我的tableView单元格中

时间:2017-08-21 10:38:56

标签: ios swift firebase firebase-realtime-database mobile-application

 ref = Database.database().reference()
 ref.child("Clubs").observe(.value, with: { (snapshot : DataSnapshot) in
        print("clubs: \(snapshot)")

        if let childSnapshot = snapshot.value as? [String : AnyObject] {
        print("myClubs: \(childSnapshot)")

从这一点来说,我无法将“name”和“bannerImage”加载到我的tableView中

if let clubName = childSnapshot["name"] as? String, let banner = childSnapshot["bannerImage"] as? String {
   print(clubName)
   print(banner)

   let club = Club(name: clubName, image: banner)
   print("new club success: \(club)")

   self.nightClubs.append(club)
   self.tableView.reloadData()
}

1 个答案:

答案 0 :(得分:1)

使用: -

 ref = Database.database().reference()

   ref.child("Clubs").observe(.value, with: { (snapshot) in
    print("clubs: \(snapshot)")

    if(snapshot.exists()) {
        let array:NSArray = snapshot.children.allObjects as NSArray

        for obj in array {
            let snapshot:FIRDataSnapshot = obj as! FIRDataSnapshot
            if let childSnapshot = snapshot.value as? [String : AnyObject] {
                print("myClubs: \(childSnapshot)")

                if let clubName = childSnapshot["name"] as? String, let banner = childSnapshot["bannerImage"] as? String {
                    print(clubName)
                    print(banner)

                    let club = Club(name: clubName, image: banner)
                    print("new club success: \(club)")
                    self.nightClubs.append(club)
                }
            }
        }
                    self.tableView.reloadData()

    }
}