从Firebase数据库获取数据到tableview

时间:2016-09-16 08:25:08

标签: ios swift firebase firebase-realtime-database

首先,我将这些值添加到我的Firebase数据库中,该数据库成功运行。

func handleSale() {
    let ref = FIRDatabase.database().reference().child("tickets")
    let childRef = ref.childByAutoId()



    guard let Price = emailTextField.text, ticketName = passwordTextField.text else {
        print("Form is not valid")
        return
    }


    let values: [String: AnyObject] = ["Price": Price, "ticketName": ticketName]

    ref.observeEventType(.ChildAdded, withBlock: { (snapshot) in

        let ticketId = snapshot.key
        let ticksRef = FIRDatabase.database().reference().child("tickets").child(ticketId)
        ticksRef.observeSingleEventOfType(.Value, withBlock: { (snapshot) in
            guard let dictionary = snapshot.value as? [String: AnyObject] else {
                return
            }

            self.messages.append(Ticket(dictionary: dictionary))

            childRef.updateChildValues(values) { (error, ref) in
                if error != nil {
                    print(error)
                    return
                }
                print(snapshot)
                print(dictionary)
                print(ticketId)
            }








            }, withCancelBlock: nil)
    })

}

然后我尝试使用自定义单元格类设置tableview并将这些值提取到tableview中,但是对于swift来说这是相当新的,我知道我还没有正确完成此操作。最后,我想我的价值观和价格#34;和" ticketName"显示在我的表格中..

这是我的tableview:

 import UIKit
  import Firebase
   class Sales: UITableViewController {
    let cellId = "cellId"

var messages = [Ticket]()
var messagesDictionary = [String: Ticket]()

override func viewDidLoad() {
    super.viewDidLoad()
    var messages = [Ticket]()
    var messagesDictionary = [String: Ticket]()

    navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Logout", style: .Plain, target: self, action: #selector(heya))

    tableView.registerClass(FredericCell.self, forCellReuseIdentifier: cellId)



    func fetchTicket() {
        FIRDatabase.database().reference().child("tickets").observeEventType(.ChildAdded, withBlock: { (snapshot) in

            if let dictionary = snapshot.value as? [String: AnyObject] {
               let ticketId = snapshot.key
             Ticket.setValuesForKeysWithDictionary(dictionary)
                return

            }

            }, withCancelBlock: nil)
    }
    print("test to see if it works")


}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 5
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier(cellId, forIndexPath: indexPath) as! FredericCell

    return cell
}



func heya() {
print ("working")
}

我编辑希望填写我的细胞,但没有运气..:

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

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier(cellId, forIndexPath: indexPath) as! FredericCell


    let ticket = tickets[indexPath.row]
    cell.textLabel?.text = ticket.ticketName
    cell.detailTextLabel?.text = ticket.Price

    dispatch_async(dispatch_get_main_queue(), {
        self.tableView.reloadData()
    })

    return cell



}

0 个答案:

没有答案