簡體   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