0
火力地堡數據庫是這個樣子如何檢索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?
}
沒有能夠檢索tableview上的數據。並且問題是獲取用戶函數在數據保存到數據庫後沒有調用過一次。 – suganya
嘗試在異步塊之外重新加載它,你的'print(字典)'行也被執行了嗎?還可以嘗試在** cellForRowAtIndexPath **中打印'noteList'並檢查它是否正確? – Dravidian
擺脫dispatch_async,因爲它不需要。將該observeEvent更改爲.Value並循環讀入的值以填充數組數據源。當該循環完成時,調用tableViewReload數據。 – Jay