在TableView Cell中显示来自Firebase的数据
我的VC中有一个代码(作为我的大型项目的一部分)中有以下代码。我的一个Firebase数据库引用被称为Sell_Request
。它有三个孩子(name
,latitude
和longitude
),但我现在只关注name
。在TableView Cell中显示来自Firebase的数据
我想将它添加到一个字符串数组,并将其打印到控制台。我看到正在打印到控制台的名称,但我没有得到单个单元格中打印的名称。
感谢您的期待。
import UIKit
import Firebase
import FirebaseDatabase
import MapKit
import CoreLocation
class RequestVC: UITableViewController, CLLocationManagerDelegate {
var locationManager = CLLocationManager()
var sellerUserNames = [String]()
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "backToMain" {
self.navigationController?.navigationBar.isHidden = true
}
}
override func viewDidLoad() {
super.viewDidLoad()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if let location = manager.location?.coordinate {
let databaseRef = FIRDatabase.database().reference()
databaseRef.child("Sell_Request").queryOrderedByKey().observe(.childAdded, with: { (FIRDataSnapshot) in
if let data = FIRDataSnapshot.value as? NSDictionary {
if let name = data[Constants.NAME] as? String {
self.sellerUserNames.append(name)
print(name)
}
}
})
}
}
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 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return sellerUserNames.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = sellerUserNames[indexPath.row]
return cell
}
/*
// Override to support conditional editing of the table view.
override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
// Return false if you do not want the specified item to be editable.
return true
}
*/
/*
// Override to support editing the table view.
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
// Delete the row from the data source
tableView.deleteRows(at: [indexPath], with: .fade)
} else if editingStyle == .insert {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
*/
/*
// Override to support rearranging the table view.
override func tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to: IndexPath) {
}
*/
/*
// Override to support conditional rearranging of the table view.
override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
// Return false if you do not want the item to be re-orderable.
return true
}
*/
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
你只需要调用tableView.reloadData()
你追加的名字到sellerUserNames
阵列后,改变你的功能,这...
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if let location = manager.location?.coordinate {
let databaseRef = FIRDatabase.database().reference()
sellerUserNames.removeAll()
databaseRef.child("Sell_Request").queryOrderedByKey().observe(.childAdded, with: { (FIRDataSnapshot) in
if let data = FIRDataSnapshot.value as? NSDictionary {
if let name = data[Constants.NAME] as? String {
self.sellerUserNames.append(name)
print(name)
self.tableView.reloadData()
}
}
})
}
}
编辑
其中/时你想打电话sellerUserNames.removeAll()
取决于你的代码和你的表视图的生命周期,但我已经编辑了我的代码上面添加它在哪里它是最li乖巧。
谢谢!这似乎是这样做的......但我似乎还有另一个问题。我得到相同的名字重复自己。我查看了一些相关的SO问题,似乎是在函数的某处调用self.sellerUserNames.removeAll()应该提供一个快速修复问题的方法,但它也只是显示名字。 – user2411290
我编辑我的答案,包括调用'sellerUserNames.removeAll()'应该解决这个问题 – MikeG
TableView
需要数据源后,重载发生在你的代码即sellerUserNames
。所以在将名称追加到sellerUserNames
之后,您需要重新加载tableview。
在追加名称后添加下面这行代码。
self.tableView.reloadData()
你可以检查你的计数是不是0.A在你的打印行重新加载主线程表。 –
嗨,你需要重新加载你的tableview。添加你的tableview名称的引用它的tableView,然后在你的if let数据语句的末尾添加self.tableView.reloadData() – webjunkie
感谢您的回复......我对这个还是有点新的。你的意思是创建一个变量(var tableView:UITableView!)或创建一个IBOutlet(@IBOutlet var tableView:UITableView!)或其他? – user2411290