将 UITableView 连接到 Firebase 结果
Posted
技术标签:
【中文标题】将 UITableView 连接到 Firebase 结果【英文标题】:Connect UITableView to Firebase results 【发布时间】:2016-04-11 00:15:17 【问题描述】:有人对 Firebase 有任何经验吗?我正在尝试将我在 firebase 中的信息显示在导航控制器中。每当我运行程序时,信息都会显示它正在被接收,因为我有打印行将它接收到的任何内容打印到命令提示符,但是会引发错误。
NSInternalInconsistencyException', reason: 'unable to dequeue a cell with identifier CellIdentifier - must register a nib or a class for the identifier or connect a prototype cell in a storyboard
我的 swift 文件在这里:
//
// All.swift
// Guaranteed Pricing
//
// Created by DePauw on 3/30/16.
// Copyright © 2016 DePauw. All rights reserved.
//
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: "cell")
self.view?.addSubview(self.tableView)
let ref = Firebase(url:"https://<firebase-id>.firebaseio.com/services")
ref.observeSingleEventOfType(.Value, withBlock: snapshot in
// 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.items.append(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)
// 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])
我想下载在以下位置找到的所有信息:
let ref = Firebase(url:"https://<firebase-id>.firebaseio.com/services")
ref.observeSingleEventOfType(.Value, withBlock: snapshot in
// 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.items.append(snapshot.key)
self.tableView!.reloadData()
)
不抛出错误。这是我的故事板:
【问题讨论】:
【参考方案1】:您使用错误的标识符注册了您的手机。将该行更改为:
self.tableView!.registerClass(UITableViewCell.self, forCellReuseIdentifier: cellIdentifier)
【讨论】:
【参考方案2】:Firebase 代码会给您带来麻烦。
当通过 .Value 观察时,请记住该节点内的所有内容都会被返回;所有孩子,孩子的孩子等。您将要遍历返回的孩子以获取键:值对
您没有发布您的结构,但假设这是使用 childByAutoId 生成的密钥
services
-Jikaijsipaij
service_name: "my service"
-kalksdokoas
service_name: "another service"
.value 返回服务中的所有内容,因此迭代子项将检索该键的每个键和子值。
ref.observeEventType(.Value, withBlock: snapshot in
for child in snapshot.children
let key = child.key as NSString //returns -Jikaijsipaij and -kalksdokoas
let name = child.value.objectForKey("service_name") as! NSString
self.items.append(key)
)
【讨论】:
每当我使用您的代码 Jay 时,我都会不断收到值和键的“对象值已解包”。这对我来说很有意义,但由于某种原因,x 代码无法读取变量键和名称。我什至尝试声明局部变量,将它们显示为字符串。 @AdrianAbles 我很抱歉 - 忘了定义 child.key 是什么:用“as NSString”更新了答案。不要忘记数据结构必须包含上面定义的 service_name。 没有代词杰伊!我真的很感谢你的帮助。我刚刚更改了 NSString,清理了项目,并更改了数据结构,但仍然发生相同的错误。还有一个错误发生在值不断发生之前,我不知道为什么。错误一直试图放置一个“?”或者 '!'后价值。 @AdrianAbles 嗯,我会说错误发生在上面的代码之外。我刚刚创建了一个新项目,输入了上面显示的 firebase 结构并复制/粘贴了代码,它就可以工作了。这 !和 ?用于选项和解包选项 - 在这种情况下我们不使用它们,并且 child.value 是 AnyObject 并且需要使用 as! 向下转换为字符串 好的!非常感谢!我将重新检查 firebase 以确保一切结构正确。以上是关于将 UITableView 连接到 Firebase 结果的主要内容,如果未能解决你的问题,请参考以下文章
无法将 UITableViewController 连接到在 xib 中创建的 UITableView
将 UITableView 单元连接到单独的视图控制器 [关闭]
将情节提要的 UITableView 连接到 .m UIViewController 作为 IBOutlet