本文介绍了如何显示我的iOS快速列表视图控制器的Firebase信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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





$ p $ 导入基础
导入UIKit
导入Firebase

class All:UINavigationController,UITableViewDataSource,UITableViewDelegate {

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

重写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 snapshot.children {child
$ b // let key = child.key // return -Jikaijsipaij and -kalksdokoas
//让name = child.value.objectForKey(service_name)作为NSString?
////
// self.items.append(key)
}
//做一些事情
print(snapshot.value)

//获取这些值a nd把它们放在单元格的文本视图中。键更重要
print(snapshot.key)



//添加到数组中,只是这个数组


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)

//取水果
let fruit = items [indexPath.row]

//配置单元格
cell.textLabel?.text = fruit
返回单元格
}

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

}

解决方案

这里有一个快速的代码片段,

  user_id_0 
名称:Fred
朋友: Barney

然后读取所有用户的代码,遍历它们并提取每个名称将其添加到数组:

  var namesArray:[String] = [] 

ref.observeSingl eEventOfType(.Value,withBlock:{快照在
中为snapshot.children中的子元素{
let name = child.value [name] as! String
namesArray.append(name)
}
self.myTableView.reloadData()
})


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

这篇关于如何显示我的iOS快速列表视图控制器的Firebase信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 14:10