如何从firebase检索数据列表并在表视图中显示它?

时间:2016-08-24 12:34:45

标签: ios swift firebase firebase-realtime-database

Firebase数据库看起来像这样

note-sam
  -KPwW5EmDbEFnlkz2XKm
      content: "abc"
      date: "2016/08/24 17:56"
      title: "there is"

  -KPwW8nXaZpx9dG59wbT
     content: "note"
     date: "2016/08/24 17:57"
     title: "first note"

我的表格视图控制器:

class TblViewController: UITableViewController {

var ref : FIRDatabaseReference!
var refHandle: UInt!
var noteList = [note]()
override func viewDidLoad() {
    super.viewDidLoad()

    ref = FIRDatabase.database().reference()
    fetchUsers()

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
       }

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


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)


    print(noteList[indexPath.row].title)
    cell.textLabel?.text = noteList[indexPath.row].title
    cell.detailTextLabel?.text = noteList[indexPath.row].date


    return cell
}

func fetchUsers()
{

     refHandl=ref.childByAutoId.observeEventType(.childAdded, withBlock: { (snapshot) in
      if let dictionary = snapshot.value as? [String : AnyObject]
       {

            print(dictionary)

            let n_note = note()

            n_note.setValuesForKeysWithDictionary(dictionary)
           self.noteList.append(n_note)

           dispatch_async(dispatch_get_main_queue(),{
                self.tableView.reloadData()
          })

       }

  })



}

}

我的note课程: -

class note: NSObject {

   var title: String?
   var content: String?
   var date: String?
}

0 个答案:

没有答案