我一直在尝试从Firebase实时数据库中检索配置文件数据,并将其显示在表格视图中,到目前为止,当我运行模拟器时,tabelview显示为空白,并且显示Failed:错误0:50 [50]显示。我很快就很新,也不确定问题出在哪里。
这是我的代码:
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)
}
}
}
答案 0 :(得分:1)
请勿在{{1}}方法中添加reloadData()
...。它将变成无限循环..因此,请从cellForRow
方法中删除self.tableView.reloadData()
答案 1 :(得分:0)
您尝试在重新加载tableView
时重新加载它。
您不应在tableView.reloadData()
方法或从同一tableView(_:cellForRowAt:)
调用的任何其他UITableViewDataSource方法中调用UITableView
。