将数据从Firebase下载到Xcode UITableView中

时间:2019-02-18 23:41:22

标签: swift firebase uitableview firebase-realtime-database

嗨,我正在尝试从Firebase数据库下载特定元素(活动,标题和纬度和经度)。拍摄数据快照并尝试选择单个元素标题时,会发生错误,提示找不到nil。

这是JSON结构

{
  "location" : {
   "-LY55OLlInZ0HLepGZWp" : {
      "Activity" : "Legs",
      "Description" : "Anainssnsj skak\nHsians\nAhah",
      "Difficulty" : "Beginner",
      "Lat" : "",
      "Long" : "",
      "Rating" : "3",
      "Title" : "Busan",
      "id" : "-LY55OLjAA3Cbcvaf8SG"
    },
    "-LY55SN53euLPN9UxoM5" : {
      "Activity" : "Board",
      "Description" : "Stktwks",
      "Difficulty" : "Beginner",
      "Lat" : "lat:-35.14202623881991",
      "Long" : "long:138.54526039212942",
      "Rating" : "3",
      "Title" : "Jettei",
      "id" : "-LY55SN40TARVvysV8fi"
    },
  },

以下是将信息保存到Firebase中的代码:

  func saveLocationInformation() {
    let key = refLocation.childByAutoId().key

    guard let title = locationTitle.text  else {return}
    guard let location = geoCoordinate.text  else {return}
    guard let long = geoLong.text else {return}
    guard let description = descriptionText.text  else {return}
    guard let rating = flameNumber.text  else {return}
    guard let difficulty = difficultyRating.text else {return}
    guard let activity = activityLabel.text else {return}


    let lTitle = [
        "id": key,
        "Title": title,
        "Activity": activity,
        "Lat": location,
        "Long": long,
        "Description": description,
        "Rating": rating,
        "Difficulty": difficulty,
    ] as [String:Any]
    let lID = [
        "id": key,
    ] as [String:Any]

    refLocation.childByAutoId().setValue(lTitle)

}

这是尝试获取数据快照并将信息放入TableView的代码

import UIKit
import Firebase
class TableViewControllerVC: UITableViewController {


 var locationData = [locationSearch]()

 var ref: DatabaseReference!
 var dataHandle: DatabaseHandle?




 override func viewDidLoad() {

     super.viewDidLoad()

     loadInfo()
 }

 override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return locationData.count
 }



 override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "TabViewCell", for: indexPath)



    return cell
}


 func loadInfo() {
    ref = Database.database().reference()
    ref.child("location").observe(.value) { (snapshot: DataSnapshot) in
        if let dict = snapshot.value as? [String: AnyObject] {
            //
            let titleT =  dict["Title"]
            //                let activityA = dict["Activity"] as! String
            //                let loc = locationSearch(titleText: titleT, activityText: activityA)
            //                self.locationData.append(loc)

            print(titleT)
            self.tableView.reloadData()
        }
    }

}




}

1 个答案:

答案 0 :(得分:1)

现在,您的观察者正在选择孩子ID 作为值,而不是他们的字典

如果您想设置一个观察者,以查找所有子级,并监听所有子级,请尝试替换"location"与此:

loadInfo()

或者,如果您希望像原始代码一样使用值快照,请使用以下这种单事件观察,而不是获取子对象列表并对其进行遍历:

func loadInfo() {
    ref = Database.database().reference()

    ref.child("location").observe(.childAdded) { (snapshot: DataSnapshot) in
        // will iterate through each child of "location" branch
        if let dict = snapshot.value as? [String: AnyObject] {
            let titleT =  dict["Title"] as? String

            print(titleT) // will return "Title" branch value
        }
    }

}