简体   繁体   中英

How to display firebase information in my iOS swift listview controller?

I am having difficulty getting firebase into my controller. Firebase is working correctly due to me being able to out print to the console. I need to be able to display all data in my app. Please assist! // // All.swift // Guaranteed Pricing //

在此处输入图片说明

import Foundation
import UIKit
import Firebase

class All: UINavigationController, UITableViewDataSource, UITableViewDelegate {

var items: [String] = []
var tableView: UITableView!
let cellIdentifier = "CellIdentifier"

override func viewDidLoad(){
    super.viewDidLoad()

    self.tableView = UITableView(frame:self.view!.frame)
    self.tableView!.delegate = self
    self.tableView!.dataSource = self
    self.tableView!.registerClass(UITableViewCell.self, forCellReuseIdentifier: cellIdentifier)
    self.view?.addSubview(self.tableView)

    let ref = Firebase(url:"https://sizzling-inferno-451.firebaseio.com/services")
    ref.observeSingleEventOfType(.Value, withBlock: { snapshot in
        for child in snapshot.children {

//                let key = child.key //returns -Jikaijsipaij and -kalksdokoas
//                let name = child.value.objectForKey("service_name") as   NSString?
////                
//                self.items.append(key)
        }
        // do some stuff once
        print(snapshot.value)

        // get these values and put them in the cell's text view. key is more important
        print(snapshot.key)



        // add to the array and just this array


        self.tableView!.reloadData()
    })
}

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


func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath)

    // Fetch Fruit
    let fruit = items[indexPath.row]

    // Configure Cell
    cell.textLabel?.text = fruit
    return cell
}

// onclick printing
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    print(items[indexPath.row])
}

}

Here's a quick snippet to populate an array within a block, and then once populated reload the tableview to display the data.

user_id_0
   name: "Fred"
   friend: "Barney"

And the code to read in all of the users, iterate over them and extract each name and add it to an array:

    var namesArray: [String] = []

    ref.observeSingleEventOfType(.Value, withBlock: { snapshot in
            for child in snapshot.children {
               let name = child.value["name"] as! String
               namesArray.append(name)
            }
            self.myTableView.reloadData()
    })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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