简体   繁体   English

iOS:在tableView中显示Firebase数据

[英]iOS: display firebase data in tableView

I try to display data from a Firebase database into my tableView. 我尝试将Firebase数据库中的数据显示到tableView中。 The code worked using "Tabs View Controller", but for the purpose of the project I had to include a TableView in a classic ViewController. 该代码使用“ Tabs View Controller”工作,但出于项目目的,我不得不在经典ViewController中包括TableView。

Here is the code that should get the data from the database, and further include it in the tableview. 这是应该从数据库获取数据,并进一步将其包含在tableview中的代码。

import UIKit
import Firebase
import FirebaseAuth
import FirebaseDatabase

class NewsfeedViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

    var ref:DatabaseReference!,
        posts = [eventStruct]()

    @IBOutlet weak var tableview: UITableView!

    struct eventStruct {
        let title: String!
        let date: String!
        let location: String!
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        loadNews()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    func loadNews() {
        ref = Database.database().reference()
        ref.child("events").queryOrderedByKey().observe(.childAdded, with: { (snapshot) in

            if let valueDictionary = snapshot.value as? [AnyHashable:String]
            {
                let title = valueDictionary["Title"]
                let location = valueDictionary["Location"]
                let date = valueDictionary["Date"]
                self.posts.insert(eventStruct(title: title, date: date, location: location), at: 0)
            }
        })
        self.tableview.reloadData()
    }

    //Table View Content
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return posts.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)

        let label1 = cell.viewWithTag(1) as! UILabel
        label1.text = posts[indexPath.row].title

        let label2 = cell.viewWithTag(2) as! UILabel
        label2.text = posts[indexPath.row].location

        let label3 = cell.viewWithTag(3) as! UILabel
        label3.text = posts[indexPath.row].date

        return cell
    }

}

I don't get any error but the data is not displayed in the tableView. 我没有收到任何错误,但数据未显示在tableView中。

Previously I had to use override for each function that controls the tableview. 以前,我必须对控制表格视图的每个函数使用重写。 If I don't have the override, I shouldn't call those functions somewhere in the code in order to have them working? 如果没有覆盖,我不应该在代码中的某些地方调用这些函数以使它们起作用? Thank you. 谢谢。

Completion block of observe will call async and your self.tableview.reloadData() will execute before that, so you need to reload the tableView inside the completion block after you insert the object in array. observe完成块将调用异步,并且您的self.tableview.reloadData()将在此之前执行,因此在将对象插入数组后,需要在完成块内重新加载tableView

func loadNews() {
    ref = Database.database().reference()
    ref.child("events").queryOrderedByKey().observe(.childAdded, with: { (snapshot) in

        if let valueDictionary = snapshot.value as? [AnyHashable:String]
        {
            let title = valueDictionary["Title"]
            let location = valueDictionary["Location"]
            let date = valueDictionary["Date"]
            self.posts.insert(eventStruct(title: title, date: date, location: location), at: 0)
            //Reload your tableView
            self.tableview.reloadData()
        }
    })

}

Note: Also confirm once that your delegate and datasource of tableView is connected. 注意:还要确认连接了tableView的delegatedatasource

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

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