![](/img/trans.png)
[英]Populating TableView with retrieved data from Firebase with Swift 3
[英]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.