我一直在尝试从 firebase 实时数据库中检索配置文件数据并将其显示在表格 View 中,到目前为止,当我运行模拟器时,tabelview 显示为空白并且 Failed : error 0:50 [50] 出现。我对 swift 很陌生,不确定问题出在哪里。
这是我的代码:
import UIKit
import FirebaseDatabase
import FirebaseStorage
import FirebaseAuth
class userViewController: UIViewController, UITableViewDelegate, UITableViewDataSource{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return profileData.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "firstName_cell",
for: indexPath)
self.tableView.reloadData()
cell.textLabel?.text = self.profileData[indexPath.item]
return cell
}
var profileData = [String]()
let storageRef = Storage.storage().reference()
var databaseRef = Database.database().reference()
var ref: DatabaseReference?
@IBOutlet var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
ref = Database.database().reference()
//let userID = Auth.auth().currentUser?.uid
ref?.child("users").observeSingleEvent( of: .childAdded, with: { (snapshot) in
let profile = snapshot.value as? String
if let actualProfile = profile{
self.profileData.append(actualProfile)
print(self.profileData)
self.tableView.reloadData()
}
}) { (error) in
print(error.localizedDescription)
}
}
}
最佳答案
不要添加 reloadData()
在 cellForRow
方法..它会变成无限循环..所以删除self.tableView.reloadData()
来自 cellForRow
方法
关于ios - Firebase 数据未显示在 xcode 的 tableview 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63348076/