ios - Firebase 数据未显示在 xcode 的 tableview 中

标签 ios swift firebase firebase-realtime-database tableview

我一直在尝试从 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/

相关文章:

android - 为删除了解析类的应用程序更新解析安装

ios - 如何在 Swift 的横向模式下更改图标的旋转?

swift - 处理 javascript 注入(inject) wkWebKit 的响应

swift - 在闭包中使用隐式数组值

android - 使用最新版本的 firebase 的问题

firebase - 如何在Flutter上使用Singleton Firebase服务?

iphone - 为 NSOperationQueue 设置并发操作只会导致该数量的操作

ios - 在 iOS 上获取星期几的名称

ios - navigationBar 标题变为默认黑色,而不是我在 viewWillAppear 中设置的颜色

ios - Firebase 数据库更改时在 iOS 中触发通知