【问题标题】:Displaying Firebase data in a tableview在表格视图中显示 Firebase 数据
【发布时间】:2020-08-14 14:15:42
【问题描述】:

我正在尝试检索存储在 Firebase 数据库中的信息,但我的表格视图未显示该信息。我尝试使用打印函数来查看是否正在检索数据,它表明是这种情况,但是当我运行模拟器时 tableview 显示为空白。

我正在使用 Xcode 11,但由于某种原因,我查看的每个教程都无法正常工作。

这是我的代码:


import UIKit
import Firebase
import FirebaseDatabase
import SwiftKeychainWrapper
import FirebaseAuth

class FeedVC: UITableViewController {
    
    var currentUserImageUrl: String!
    var posts = [postStruct]()
    var selectedPost: Post!

    override func viewDidLoad() {
        super.viewDidLoad()
        getUsersData()
        getPosts()
        // Do any additional setup after loading the view.
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    
    func getUsersData(){

        guard let userID = Auth.auth().currentUser?.uid else { return }
        Database.database().reference().child("users").child(userID).observeSingleEvent(of: .value) { (snapshot) in
            if let postDict = snapshot.value as? [String : AnyObject] {
                self.tableView.reloadData()
            }
        }
    }
    
    struct postStruct {
        let firstName : String!
        let lastName : String!
    }
    
    func getPosts() {
        let databaseRef = Database.database().reference()
        databaseRef.child("profiles").queryOrderedByKey().observeSingleEvent(of: .childAdded, with: {
                    snapshot in
                    let firstName = (snapshot.value as? NSDictionary)!["profileForename"] as? String
                    let lastName = (snapshot.value as? NSDictionary
                        )!["profileSurname"] as? String
                    print(firstName)
                    self.posts.append(postStruct(firstName: firstName, lastName: lastName))
                    print(self.posts)
                    DispatchQueue.main.async {
                        self.tableView.reloadData()
                    }
                })
    }
    
    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return posts.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        guard let cell = tableView.dequeueReusableCell(withIdentifier: "PostCell") as? PostCell else { return UITableViewCell() }
        
        let nameLabel = cell.viewWithTag(1) as! UILabel
        nameLabel.text = posts[indexPath.row].firstName
        return cell
    }



}

任何帮助将不胜感激!

【问题讨论】:

    标签: swift xcode firebase tableview


    【解决方案1】:

    更新:由于PostCell 是在表格视图的情节提要中创建的,因此它已成功注册和出列。所以问题被缩小到标签为 1 的标签上。尝试为标签创建一个@IBOutlet 并使用它来设置UILabel 的文本。

    然后在cellForRowAt:

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
        guard let cell = tableView.dequeueReusableCell(withIdentifier: "PostCell") as? PostCell else { return UITableViewCell() }
        cell.firstNameLabel.text = posts[indexPath.row].firstName
        return cell
    }
    

    上一篇:你好像忘了registerPostCell

    override func viewDidLoad() {
        super.viewDidLoad()
        //...
        tableView.register(PostCell.self, forCellReuseIdentifier: "PostCell")
    }
    

    注意:如果您在 Xib 中创建了PostCell,请使用 nib 注册表方法。

    更新:如果您想使用 Nib 方法注册:

    tableView.register(UINib(nibName: <#T##String#>, bundle: nil), forCellReuseIdentifier: "PostCell") // provide the xib file name at the placeholder
    

    【讨论】:

    • 感谢您的快速回复,我对使用 Swift 很陌生 - 我将如何使用这些 nib 注册表方法?
    • 我已经实现了代码,但是表格视图仍然显示为空白...
    • 确保在您创建 PostCell 的 Xib 文件中提供了 reuseIdentifier
    • 将单元格声明行替换为:guard let cell = tableView.dequeueReusableCell(withIdentifier: "PostCell") as? PostCell else { print("Couldn't get cell"); return UITableViewCell() } 看看你是否在日志中得到了什么。
    • 日志中没有打印“无法获取单元格”
    猜你喜欢
    • 2020-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多