简体   繁体   English

从Firebase在Tableview中快速显示数据

[英]swift show data in Tableview from Firebase

Im trying to retrieve data from firebase and store it into array and show it in tableView but after running the code the tableView does not show anything so whats the problem do i missed something ,somehow my tableView reloadData dose not work and my Array seem have values in method but outside the method the array is empty , I'm using Swift 4 . 我试图从firebase中检索数据并将其存储到数组中并在tableView显示,但是在运行代码后tableView没有显示任何内容,所以什么问题我错过了什么,以某种方式我的tableView reloadData无法正常工作并且我的数组似乎有值在方法中,但在方法之外,数组为空,我使用的是Swift 4。

 import UIKit
 import  Firebase
 import  ProgressHUD
class HistoryTableViewController: UITableViewController  {

var keys = ""
var arrayHistory : [String] =  []

@IBOutlet var hist: UITableView!


func RET  (){
    print("!!!!!!!!!!!!!!!!!!!!!")
   let messageDB = Database.database().reference().child("history")


    messageDB.observe(.childAdded) { (snapshot) in
        print("xxxxxxxxxxxxxxxxxxxxxxxxxxxx")

        let h = snapshot.value as! String

        self.arrayHistory.append(h)

        print(h)

        self.tableView.reloadData()

         ProgressHUD.dismiss()
    }

}

override func viewDidLoad() {

    super.viewDidLoad()

    ProgressHUD.show()

    RET()

    print(" basimmmmmmmmmmm")

}

  override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

// MARK: - Table view data source

override func numberOfSections(in tableView: UITableView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return 0
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of rows
    return arrayHistory.count
}

 override func tableView(_ tableView: UITableView, cellForRowAt 
   indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier", for: indexPath)

    // Configure the cell...


   print ("hlooooooooooooooooo")

   cell.textLabel!.text = arrayHistory[indexPath.row]

    return cell
}
}

You have two options, you delete this method: 您有两个选择,可以删除此方法:

override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
    return 0
}

OR you modify it to return your desired sections number: 或者,您可以对其进行修改以返回所需的部分编号:

override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
    return yourCount // whatever section count you want
}

If you only delete it it will return automatically 1 for number of sections in table view. 如果仅删除它,则表视图中的节数将自动返回1。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM