从Firebase中检索数据到tableview [英] Retrieving data from Firebase to tableview
问题描述
在一个视图中,我的用户在输入字段中输入一些文本,保存为票据
(这个过程没有问题,并添加到我的数据库成功)。
我希望所有这些票据
值在 tableView
,其中每个单元格包含一个票据
。我一直在玩,这是我可以去,但它仍然是不够的。
import UIKit
import Firebase
class TestTableViewController:UITableViewController {
let cellNavn =cellNavn
var holes:[FIRDataSnapshot]! = []
让CoursesRef = FIRDatabase.database()。reference()。child(Ticketcontainer)
override func viewDidLoad ){
super.viewDidLoad()
CoursesRef.observeEventType(.ChildAdded,withBlock:{snapshot in
self.holes = snapshot.childSnapshotForPath(tickets)。children。 allObjects as![FIRDataSnapshot]
})
navigationItem.leftBarButtonItem = UIBarButtonItem(title:Cancel,style:.Plain,target:self,action:#selector(handleCancel))
tableView.registerClass(UserCell.self,forCellReuseIdentifier:cellNavn)
$ b func handleCancel(){
dismissViewControllerAnimated(true,completion:nil)
}
覆盖func tableView(tableView:UITableView,numberOfRowsInSection section:Int) - > Int {
return 2
}
覆盖func tableView(tableView:UITableView,cellForRowAtIndexPath indexPath:NSIndexPath) - > UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(cellNavn,forIndexPath:indexPath)as! UserCell
cell.textLabel?.text = self.holes [indexPath.row] .value as?字符串
返回单元格
}
覆盖func tableView(tableView:UITableView,heightForRowAtIndexPath indexPath:NSIndexPath) - > CGFloat {
return 72
}
}
override func tableView(tableView:UITableView,numberOfRowsInSection section:Int) - > Int {
return holes.count
}
第二次初始化数组您需要重新加载 tableView
。
CoursesRef.observeEventType(。 ChildAdded,withBlock {$ snapshot $ $ $ self.holes = snapshot.childSnapshotForPath(tickets)。children.allObjects as![FIRDataSnapshot]
self.tableView.reloadData()
})
In a view, my user types some text in a input field, which I save as a ticket
(this process works without problems and are added to my database successfully).
I want all these ticket
values to be presented in a tableView
in which each cell contains a ticket
. I've been playing around, and this is as far as I could go, but it is still not enough.
import UIKit
import Firebase
class TestTableViewController: UITableViewController {
let cellNavn = "cellNavn"
var holes: [FIRDataSnapshot]! = []
let CoursesRef = FIRDatabase.database().reference().child("Ticketcontainer")
override func viewDidLoad() {
super.viewDidLoad()
CoursesRef.observeEventType(.ChildAdded, withBlock: { snapshot in
self.holes = snapshot.childSnapshotForPath("tickets").children.allObjects as! [FIRDataSnapshot]
})
navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Cancel", style: .Plain, target: self, action: #selector(handleCancel))
tableView.registerClass(UserCell.self, forCellReuseIdentifier: cellNavn)
}
func handleCancel() {
dismissViewControllerAnimated(true, completion: nil)
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 2
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(cellNavn, forIndexPath: indexPath) as! UserCell
cell.textLabel?.text = self.holes[indexPath.row].value as? String
return cell
}
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 72
}
}
First of all in numberOfRowsInSection
method you need to return your array's count instead of some static value.
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return holes.count
}
Second after initializng your array in closure you need to reload the tableView
.
CoursesRef.observeEventType(.ChildAdded, withBlock: { snapshot in
self.holes = snapshot.childSnapshotForPath("tickets").children.allObjects as! [FIRDataSnapshot]
self.tableView.reloadData()
})
这篇关于从Firebase中检索数据到tableview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!