[The Table View is not showing any values that were defined in firebase.][1]
[Firebase已连接。当我在调试控制台中执行Print对象时,正在获取定义的数据]
https://i.stack.imgur.com/cZuKb.png
**请随意组织和整理,我对这件事很陌生。
谢谢
**
//导入
import UIKit
import Firebase
import FirebaseDatabase
class DatabaseViewController: UIViewController, UITableViewDelegate,
UITableViewDataSource {
override func viewDidLoad() {
// Do any additional setup after loading the view.
//Firebase Refernece
ref = FIRDatabase.database().reference()
databaseHandle = ref?.child("CandleInventory").observe(.childAdded,
with: { (snapshot) in
//Execute Snapshot
let candle = snapshot.value as! String?
if let actualCandle = candle {
self.candleData.append(actualCandle)
}
})
tableView.reloadData()
}
@IBOutlet weak var tableView: UITableView!
var ref: FIRDatabaseReference!
var databaseHandle:FIRDatabaseHandle?
var candleData = [String]()
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
public func tableView(_ tableView: UITableView, numberOfRowsInSection
section: Int) -> Int {
return candleData.count
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath:
IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: UITableViewCellStyle.default,
reuseIdentifier: "cell")
cell.textLabel?.text = candleData[indexPath.row]
return(cell)
}
//Datajust wont output
}
最佳答案
我非常确定您要从观察者重新加载数据,即在数据被附加之后:
databaseHandle = ref?.child("CandleInventory").observe(
.childAdded,
with: { [weak self] (snapshot) in
guard let `self` = self else { return }
let candle = snapshot.value as! String?
guard let actualCandle = candle else { return }
self.candleData.append(actualCandle)
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
)
关于ios - TableView未显示来自Firebase的数据,但在“po快照”调试控制台下显示了获取数据,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46860211/