繁体   English   中英

从Firebase在Tableview中快速显示数据

[英]swift show data in Tableview from Firebase

我试图从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
}
}

您有两个选择,可以删除此方法:

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

或者,您可以对其进行修改以返回所需的部分编号:

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

如果仅删除它,则表视图中的节数将自动返回1。

暂无
暂无

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

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