从Firebase Swift检索数据到Tableview

时间:2017-12-16 14:30:54

标签: swift uitableview firebase-realtime-database



{
  "Firebase" : {
    "AnotherTest" : "Test",
    "Class" : "Missed",
    "Owner" : "Lenovo",
    "Test" : "Folder"
  },
  "Posts" : {
    "-L0TR_W8Ao671XEGNY2O" : {
      "message" : "Message",
      "title" : "Title"
    }
  },
  "usersID" : {
    "SEdMgVirYTQ5x02BVP1a13oCu5n2" : {
      "info" : {
        "email" : "test@test.dk"
      }
    },
    "TcK0fcoRKxcOIlDBNrbGvpEN28H3" : {
      "class" : {
        "info" : "demo",
        "owner" : "airclass",
        "participant" : "ac-mobilie team",
        "time" : "15. dec @ 5:00 PM"
      },
      "info" : {
        "email" : "steen@steen.dk"
      }
    }
  }
}




我有点像新手,尤其是Firebase:P 我试图将一些数据检索到tableview,但我真的很难让我的快照工作,有人可以帮忙吗?

[我的代码串来自X代码。] [2]

导入UIKit 导入Firebase 导入FirebaseDatabase

struct postStruct {     let title:String!     让消息:字符串! }

class MissedTableViewController:UITableViewController {

let posts = [postStruct]()

var dbRef: DatabaseReference!

let userID = Auth.auth().currentUser?.uid


override func viewDidLoad() {
    super.viewDidLoad()

    self.dbRef.child("Posts").child(userID!).observeSingleEvent(of: .value, with: { (snapshot) in
        // Get user value
        let value = snapshot.value as? NSDictionary
        //print("user data: \(value!)") //Dette er et JSON objekt oversat til et NSDictionary!

        let valueOfInfo = value?["Posts"] as? [String: Any]

        let title = "title: " + (valueOfInfo?["title"] as? String ?? "")
         let message = "message: " + (valueOfInfo?["message"] as? String ?? "")
        self.tableView.reloadData()
    })


    // Do any additional setup after loading the view.
    dbRef = Database.database().reference()
    post()
}


func post(){
    let title = "Title"
    let message = "Message"

    let post : [String : AnyObject] = ["title": title as AnyObject, "message": message as AnyObject]
    // Her siger man hvad man gerne vil have ind i databasen.

    dbRef.child("usersID").child(userID!).setValue(post) // Her indsætter man det på "usersID" række
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


// MARK: - Table view data source

override func numberOfSections(in tableView: UITableView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return 0
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of rows
    return self.posts.count
}

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

    // Her indsættes dataen i cellen.

    return cell

}


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

    // Configure the cell...

    return cell
}
*/

/*
// Override to support conditional editing of the table view.
override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
    // Return false if you do not want the specified item to be editable.
    return true
}
*/

/*
// Override to support editing the table view.
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
    if editingStyle == .delete {
        // Delete the row from the data source
        tableView.deleteRows(at: [indexPath], with: .fade)
    } else if editingStyle == .insert {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }    
}
*/

/*
// Override to support rearranging the table view.
override func tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to: IndexPath) {

}
*/

/*
// Override to support conditional rearranging of the table view.
override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
    // Return false if you do not want the item to be re-orderable.
    return true
}
*/

/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    // Get the new view controller using segue.destinationViewController.
    // Pass the selected object to the new view controller.
}
*/

}

0 个答案:

没有答案