简体   繁体   English

我想在tableView中显示来自firebase的数据

[英]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. 我想从我的firebase实时数据库中获取数据并将其放入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. 在每个tableView单元格中,我想查看我的俱乐部名称,然后是日期。 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. 我的TableViewControllers标题是“Cell”,但我在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. 您应该为您的单元格提供“Cell”的重用标识符,因为这是您要获取的内容。

  2. numberOfRowsInSection is asking for how many rows your table will have, in your case this should be posts.count . numberOfRowsInSection要求你的表有多少行,在你的情况下,这应该是posts.count

  3. In numberOfSections try just returning 1. numberOfSections尝试返回1。

  4. Finally, create a class for your TableViewCell to properly set the labels values. 最后,为TableViewCell创建一个类来正确设置标签值。 Simple explanation here . 这里简单解释一下

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

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