2016-04-11 24 views
-1

Firebaseの経験がある人はいますか?私はfirebase内にある情報をナビゲーションコントローラ内に表示しようとしています。私はプログラムを実行するたびに、情報が表示されていることを示しています。なぜなら、コマンドラインに何があっても印刷ラインが印刷されるからです。しかし、エラーがスローされます。FireToneの結果にUITableViewを接続してください

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 

私の迅速なファイルはここにある:エラーをスローせずに

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

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

は私がで見つかったすべての情報をダウンロードします。

答えて

0

は、あなたが間違った識別子を使用してセルを登録:これが私のストーリーボードです。その行を次のように変更してください:

self.tableView!.registerClass(UITableViewCell.self, forCellReuseIdentifier: cellIdentifier) 
0

そのFirebaseコードが問題を引き起こす原因になります。

.Valueを観察するときは、そのノード内のすべてが返されることに注意してください。あなたの構造を投稿したが

services 
    -Jikaijsipaij 
    service_name: "my service" 
    -kalksdokoas 
    service_name: "another service" 
childByAutoId

で生成されたキーでこれを行うものではありませんでした

値のペア:すべての子供たちは、あなたが子供を反復処理したいしようとしている子どもなどの子どもたちは、キーを取得するために戻りました

.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) 
    } 
}) 
+0

私はあなたのコードジェイを使用するたびに、値とキーの "オブジェクトの値がラップされていません"を取得し続けます。それは私には意味がありますが、何らかの理由でxコードが変数のキーと名前を読み込むのに問題があります。私はそれらを文字列として示すローカル変数を宣言しようとしました。 –

+0

@AdrianAbles私のお詫び - どのchild.keyが何であるかを定義するのを忘れました: 'NSString'として答えを更新しました。上で定義したように、データ構造にservice_nameが含まれていなければならないことを忘れないでください。 – Jay

+0

no pronlem Jay!私は本当にあなたの助けに感謝します。私はちょうどNSStringを変更し、プロジェクトをきれいにし、データ構造を変更しましたが、同じエラーがまだ発生しています。また、値の直前で発生しているエラーが発生していると私はなぜそれがわからない。エラーは「?」を配置しようとし続けます。または '!'値の後に。 –

関連する問題