iOS: display firebase data in tableView

落花浮王杯 提交于 2021-02-20 04:49:48

问题


I try to display data from a Firebase database into my 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.

Here is the code that should get the data from the database, and further include it in the 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.

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.


回答1:


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.

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.



来源:https://stackoverflow.com/questions/44499210/ios-display-firebase-data-in-tableview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!