使用4个labell将数据从Firebase检索到自定义表格视图cel中

时间:2019-07-11 15:57:51

标签: ios swift xcode firebase

what I need to displayfirebase structure 我在此代码上遇到一些问题,我需要从firebase获取日期,然后在自定义表格视图单元格中显示,这里是我的编码,我正在使用nsobject,这是我的编码 我被困在如何将Firebase的字符串值附加到这些标签中。请帮忙。

import UIKit
import FirebaseDatabase
class viewbus: UIViewController,UITableViewDelegate,UITableViewDataSource{

    @IBOutlet weak var tableview: UITableView!

    var ref : DatabaseReference?
    var  databasehandle : DatabaseHandle?
    var viewdata1 : [viewdata] = []

    override func viewDidLoad() {
        super.viewDidLoad()
        tableview.dataSource = self
        tableview.delegate = self
        ref = Database.database().reference()
        databasehandle =  ref?.child("Departure").observe(.childAdded, with: { (snapshot) in
            let view = snapshot.value as? [viewdata]
            if  let actualview = view {
                self.viewdata1.append (viewdata())
                self.tableview.reloadData()
            }
        })

        databasehandle =  ref?.child("Arrival").observe(.childAdded, with: { (snapshot) in
            let view = snapshot.value as? [viewdata]
            if  let actualview = view {
                self.viewdata1.append (viewdata())
                self.tableview.reloadData()
            }
        })

        databasehandle =  ref?.child("Price").observe(.childAdded, with: { (snapshot) in
            let view = snapshot.value as? [viewdata]
            if  let actualview = view {
                self.viewdata1.append (viewdata())
                self.tableview.reloadData()
            }
        })

        databasehandle =  ref?.child("Time").observe(.childAdded, with:  { (snapshot) in
            let view = snapshot.value as? [viewdata]
            self.viewdata1.append (viewdata())
            self.tableview.reloadData()
        })
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return viewdata1.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "viewcell") as! busTableViewCell
        let data = viewdata1[indexPath.row]
        cell.lb1.text = "deperture: \(viewdata1.Departure)"
        return cell
    }
}

//这是nsobject

    import UIKit
    import Foundation
    class viewdata: NSObject {

    var Departure : String
    var Arrival : String
    var Price : String
    var Time : String


init(Departure : [String:AnyObject],Arrival : String ,Price : String, Time : String) {

        self.Departure = Departure["Departure"] as! String
        self.Arrival = Arrival
        self.Price = Price
        self.Time = Time
           }
         }

//自定义tableviewcell

        import UIKit

        class busTableViewCell: UITableViewCell {

@IBOutlet weak var lb1: UILabel!
@IBOutlet weak var lb2: UILabel!
@IBOutlet weak var lb3: UILabel!

@IBOutlet weak var lb4: UILabel!
override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
       }


      }

0 个答案:

没有答案