简体   繁体   中英

I want to display data from firebase in a tableView

First Off I know this question has been asked but all the solutions I've tried hasn't worked. I want to take data from my firebase real-time database and put it into a tableView. My database is set up like this:


Clubs- 
  Club1- 
     Name- Club1 
     date- 5/09/18

In each tableView cell, I want to see my club name followed by the date. My code compiles fine but dosen't do anything. My TableViewControllers Title is "Cell" but I don't have a reuse identifier on the tableViewCell.

//
//  CoolTable.swift
//  
//
//  Created by AISD on 4/2/19.
//

import UIKit
import Firebase
import GoogleSignIn
var refa: DatabaseReference!

class CoolTable: UITableViewController{
    var posts = [eventStruct]()

    @IBOutlet var tableview: UITableView!
    override func viewDidLoad() {
        super.viewDidLoad()
        refa = Database.database().reference()
        loadNews()
        tableview.delegate = self
        tableview.dataSource = self


    }
    struct eventStruct {
        let name: String!
        let date: String!
    }

    func loadNews() {
        refa.child("Club").queryOrderedByKey().observe(.childAdded, with: { (snapshot) in

            if let valueDictionary = snapshot.value as? [AnyHashable:String]
            {
                let name = valueDictionary["Name"]
                let date = valueDictionary["date"]
                self.posts.insert(eventStruct(name: name, date: date), at: 0)
                self.tableview.reloadData()


            }
        })
    }

    // MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 0
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return 0
    }

    override 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].name



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

        return cell
    }


}
  1. You should give your cell a reuse identifier of "Cell" since that is what you're trying to get.

  2. numberOfRowsInSection is asking for how many rows your table will have, in your case this should be posts.count .

  3. In numberOfSections try just returning 1.

  4. Finally, create a class for your TableViewCell to properly set the labels values. Simple explanation here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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