Swift Firebase将数据检索到tableview中

时间:2017-10-03 13:13:40

标签: swift uitableview firebase firebase-realtime-database

我的数据库树

enter image description here

您好, 我正在尝试将数据检索到tableview但是虽然我可以从firebase数据库中读取数据,但我无法在表视图中显示它们。我的代码如下,我希望你能帮助我。

class Calls {

var callType: String?
var callHospital: String?

init(callType: String?, callHospital: String?) {
    self.callType = callType
    self.callHospital = callHospital
}
}

class myCallsViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

var ref:DatabaseReference!
var myCallList = [Calls]()

@IBOutlet weak var callListTableView: UITableView!

func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

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

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "customcell", for: indexPath) as! myCallsViewControllerTableViewCell

    let test = myCallList[indexPath.row]
    cell.callType?.text = test.callType
    cell.callHospital?.text = test.callHospital

    return cell
}

override func viewDidLoad() {
    super.viewDidLoad()

     callListTableView.dataSource = self
     callListTableView.delegate = self
   LoadCalls()
}

func LoadCalls() {
    ref = Database.database().reference()
    let userID = Auth.auth().currentUser?.uid
    ref.child("calls").queryOrdered(byChild: "userID").queryEqual(toValue: userID!).observe(.childAdded, with: { (snapshot) in
        if snapshot.childrenCount > 0{
        self.myCallList.removeAll()
            for result in snapshot.children.allObjects as! [DataSnapshot]{
                let results = result.value as? [String : AnyObject]
                let type = results?["calltype"]
                let hospital = results?["hospital"]
                let myCalls = Calls(callType: type as! String?, callHospital: hospital as! String?)
                self.myCallList.append(myCalls)
        }
                 self.callListTableView.reloadData()
        }
        })
}

2 个答案:

答案 0 :(得分:2)

我解决了这个问题,谢谢你们,Blake和Siyavash,这么多。我注册了单元格并放置了调度主队列并且工作正常。这是最新的代码: class Call {

var callType: String?
var callHospital: String?

init(callType: String?, callHospital: String?) {
    self.callType = callType
    self.callHospital = callHospital
}

}

class myCallsViewController:UIViewController,UITableViewDelegate,UITableViewDataSource {

var ref:DatabaseReference!
var myCallList = [Calls]()

@IBOutlet weak var callListTableView: UITableView!

func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

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

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "customcell", for: indexPath) as! myCallsViewControllerTableViewCell

    let test = myCallList[indexPath.row]
    cell.callType?.text = test.callType
    cell.callHospital?.text = test.callHospital

    return cell
}

override func viewDidLoad() {
    super.viewDidLoad()

     callListTableView.dataSource = self
     callListTableView.delegate = self
   LoadCalls()
}

func LoadCalls() {
    ref = Database.database().reference()
    let userID = Auth.auth().currentUser?.uid
    ref.child("calls").queryOrdered(byChild: "userID").queryEqual(toValue: userID!).observe(.childAdded, with: { (snapshot) in
                let results = snapshot.value as? [String : AnyObject]
                let type = results?["calltype"]
                let hospital = results?["hospital"]
                let myCalls = Calls(callType: type as! String?, callHospital: hospital as! String?)
                self.myCallList.append(myCalls)
                DispatchQueue.main.async {
                    self.callListTableView.reloadData()
                }
        })
}

答案 1 :(得分:0)

您的问题可能与您从闭包中调用reloadData()这一事实有关,这意味着您要从后台线程更新UI。看看这个答案:

Swift UITableView reloadData in a closure