【问题标题】:Firebase data not displaying in tableview in xcodeFirebase 数据未显示在 xcode 的 tableview 中
【发布时间】:2020-11-30 12:33:15
【问题描述】:

我一直在尝试从 firebase 实时数据库中检索配置文件数据并将其显示在表格视图中,到目前为止,当我运行模拟器时,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)
            }


            
        }
        
    }

【问题讨论】:

    标签: ios swift firebase firebase-realtime-database tableview


    【解决方案1】:

    不要在cellForRow方法中添加reloadData()....它会变成无限循环..所以从cellForRow方法中删除self.tableView.reloadData()

    【讨论】:

    • 为了更加清晰,在调用 reloadData() 时多次调用 cellForRow 以设置每个单元格。
    • 好的,我已经完成了,但仍然没有数据显示。我确保数据库规则设置为 true。这是输出:'App Delegate 不符合 UIApplicationDelegate 协议。没有待激活的快照。 [图形] 使用远远超出预期范围的组件值创建的 UIColor。在 UIColorBreakForOutOfRangeColorComponents 上设置断点进行调试。此消息只会记录一次。
    • 是的..你需要设置tableview的委托和数据源
    • profileData 为空,但 self.profileData.append(actualProfile) 应将来自 firebase 的数据添加到其中
    • 但它没有附加?
    【解决方案2】:

    您尝试在重新加载 tableView 时重新加载它。

    您不应在tableView(_:cellForRowAt:) 方法中调用tableView.reloadData() 或从同一UITableView 调用的任何其他UITableViewDataSource 方法。

    【讨论】:

    • 好的,我已经完成了,但仍然没有数据显示。我确保数据库规则设置为 true。这是输出:'App Delegate 不符合 UIApplicationDelegate 协议。没有待激活的快照。 [图形] 使用远远超出预期范围的组件值创建的 UIColor。在 UIColorBreakForOutOfRangeColorComponents 上设置断点进行调试。此消息只会记录一次。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-13
    • 2018-04-07
    • 2017-01-06
    • 2019-10-08
    相关资源
    最近更新 更多