繁体   English   中英

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

[英]I want to display data from firebase in a tableView

首先我知道这个问题已被问到,但我尝试过的所有解决方案都没有用。 我想从我的firebase实时数据库中获取数据并将其放入tableView中。 我的数据库设置如下:


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

在每个tableView单元格中,我想查看我的俱乐部名称,然后是日期。 我的代码编译得很好但不做任何事情。 我的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. 您应该为您的单元格提供“Cell”的重用标识符,因为这是您要获取的内容。

  2. numberOfRowsInSection要求你的表有多少行,在你的情况下,这应该是posts.count

  3. numberOfSections尝试返回1。

  4. 最后,为TableViewCell创建一个类来正确设置标签值。 这里简单解释一下

暂无
暂无

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

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