2016-08-24 102 views
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? 
} 
+0

没有能够检索tableview上的数据。并且问题是获取用户函数在数据保存到数据库后没有调用过一次。 – suganya

+0

尝试在异步块之外重新加载它,你的'print(字典)'行也被执行了吗?还可以尝试在** cellForRowAtIndexPath **中打印'noteList'并检查它是否正确? – Dravidian

+0

摆脱dispatch_async,因为它不需要。将该observeEvent更改为.Value并循环读入的值以填充数组数据源。当该循环完成时,调用tableViewReload数据。 – Jay

回答

相关问题