ios - 如何在我的 iOS swift listview Controller 中显示 firebase 信息?

标签 ios xcode swift firebase

我很难将 firebase 装入我的 Controller 。由于我能够将打印输出到控制台,Firebase 工作正常。我需要能够在我的应用程序中显示所有数据。请协助! // //所有.swift //保证定价 //

enter image description here

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])
}

}

最佳答案

这是一个快速片段,用于填充 block 内的数组,然后填充后重新加载表格 View 以显示数据。

user_id_0
   name: "Fred"
   friend: "Barney"

以及读取所有用户、迭代它们并提取每个名称并将其添加到数组中的代码:

    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()
    })

关于ios - 如何在我的 iOS swift listview Controller 中显示 firebase 信息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36586755/

相关文章:

ios - "Not running"状态下的远程通知

iphone - 如何加载已删除的 xib?

ios - 反序列化 SIL 全局 "UIEdgeInsetsZero"时遇到 fatal error

swift - 我可以通过将可选参数用作参数来检查可选参数是否为 nil 吗?

ios - Xcode - 为运行和测试操作设置不同的类

ios - uitextfield selected 更改状态栏颜色

iphone - 用于比较 Xcode 项目文件的工具

ios - armv7 未包含在构建中

ios - 有没有办法通知服务器应用程序已终止?

ios - 在textview ios中逐字删除而不是逐字符删除