繁体   English   中英

TableView不显示Firebase数据库中的数据

[英]TableView not displaying data from Firebase Database

我正在构建一个使用Firebase数据库服务的应用程序。 我正在尝试将数据加载到表视图中,但无法这样做。 我似乎无法找出问题所在。 该代码也没有给我任何错误。 我检查了Firebase上的数据库权限,它们似乎很好。 这是我的代码:

    import UIKit
import Firebase

struct postStruct {
    let word : String!
    let wordType : String!
}

class sentenceBuilderViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    @IBOutlet weak var wordSearchBar: UISearchBar!
    @IBOutlet weak var wordsTableView: UITableView!

    var posts = [postStruct]()
    override func viewDidLoad() {
        wordsTableView.reloadData()
        getWordsFromDatabase()
        super.viewDidLoad()
        wordsTableView.delegate = self
        wordsTableView.dataSource = self
    }

    func getWordsFromDatabase() {
        let databaseRef = Database.database().reference()
            databaseRef.child("wordList").queryOrderedByKey().observeSingleEvent(of: .childAdded, with: {
                snapshot in


                let word = (snapshot.value as? NSDictionary)!["word"] as? String
                let wordType = (snapshot.value as? NSDictionary
                    )!["wordType"] as? String
                self.posts.insert(postStruct(word: word, wordType: wordType), at: 0)

            })
        wordsTableView.reloadData()
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
       return posts.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        var cell = wordsTableView.dequeueReusableCell(withIdentifier: "Cell")

        let wordLabel = cell?.viewWithTag(1) as! UILabel
        wordLabel.text = posts[indexPath.row].word

        let wordTypeLabel = cell?.viewWithTag(2) as! UILabel
        wordTypeLabel.text = posts[indexPath.row].wordType

        return cell!
    }

}

任何帮助和投入将不胜感激! 谢谢!

在分配了委托和数据源之后,将“ viewDidLoad”函数中的“ getWordsFromDatabase()”行移至以下位置:

override func viewDidLoad() {
    super.viewDidLoad()
    wordsTableView.delegate = self
    wordsTableView.dataSource = self
    getWordsFromDatabase()
}

您也可以尝试在主队列的databaseRef块中添加“ reloadData()”方法,如下所示:

let databaseRef = Database.database().reference()
databaseRef.child("wordList").queryOrderedByKey().observeSingleEvent(of: .childAdded, with: {
            snapshot in
            let word = (snapshot.value as? NSDictionary)!["word"] as? String
            let wordType = (snapshot.value as? NSDictionary
                )!["wordType"] as? String
            self.posts.insert(postStruct(word: word, wordType: wordType), at: 0)
            DispatchQueue.main.async {
                wordsTableView.reloadData()
            }
        })

问题是您只是在这里观察一个事件:

databaseRef.child("wordList").queryOrderedByKey().observeSingleEvent(of: .childAdded, with: {
            snapshot in

这是因为它会遍历您的数据库,并且一旦找到任何子级,它便会显示该子级而不会进一步。 您需要做的是将其更改为像这样观察:

 func getAllWordsFromDatabase() {
    let databaseRef = Database.database().reference()
    databaseRef.child("wordList").queryOrderedByKey().observe(.childAdded, with: {
        snapshot in
        let word = (snapshot.value as? NSDictionary)!["word"] as? String
        let wordType = (snapshot.value as? NSDictionary)!["wordType"] as? String
        self.posts.append(postStruct(word: word, wordType: wordType))

        DispatchQueue.main.async {
            self.wordsTableView.reloadData()
        }
        })
}

尝试实现这一点,它应该可以工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM