2017-11-21 6 views
0

私はSwiftを初めて利用しています。私はFirebaseから単一のデータを取得する方法を知っていますが、データのリストを配列に取得しようとすると、エラーもデータもありません。私を助けてください。私は今これで数日間苦労しています。 Firebaseのデータをアレイに追加したい、 カテゴリのリストでjsonファイルを作成し、firebaseにインポートしました。Firebaseからのデータのリストをどのようにしてアレイに入れるのですか

私のJSONファイルは、次のようになります。Categoryクラスは、私はここに私のデータを表示するカスタムセルを使用して、この

struct Category { 

    private(set) public var title: String 
    private(set) public var imageName: String 


    init(title: String, imageName: String) { 
     self.title = title 
     self.imageName = imageName 
    } 
} 

のように見えます

this よう

{ 
    "Category" : [ { 
    "categoryId" : "1", 
    "imageName" : "cat_001.png", 
    "title" : "CAT" 
    }, { 
    "categoryId" : "2", 
    "imageName" : "dog_001.png", 
    "title" : "DOG" 
    }, { 
    "categoryId" : "3", 
    "imageName" : "fish_001.png", 
    "title" : "FISH" 
    }, { 
    "categoryId" : "4", 
    "imageName" : "bird_001.png", 
    "title" : "BRID" 
    }] 
} 

Firebaseデータベースが見えます私のカスタムセルクラスです

class CategoryCell: UITableViewCell { 

    @IBOutlet weak var categoryImage: UIImageView! 
    @IBOutlet weak var categoryTitle: UILabel! 

    func updateViews(category: Category){ 
     categoryImage.image = UIImage(named: category.imageName) 
     categoryTitle.text = category.title 
    } 
} 

私はDataServiceクラスを使用してデータを取得していますが、現在データはハードコードされており、正常に動作しています。ここ

class DataService{ 
     static let instance = DataService() 

// How to add data from firebase in here`? 
     private let categories = [Category(title: "CAT", imageName: "cat_001"), 
            Category(title: "DOG", imageName: "dog_001"), 
            Category(title: "FISH", imageName: "fish_001")] 

     func getCategories() -> [Category]{ 
      return categories 

     } 
    } 

と最終的に私は将来的にはより多くのカテゴリを追加するつもりです私のViewController

class CategoriesVC: UIViewController, UITableViewDataSource, UITableViewDelegate { 

    @IBOutlet weak var categoryTable: UITableView! 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     categoryTable.dataSource = self 
     categoryTable.delegate = self 
    } 

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return DataService.instance.getCategories().count 
    } 

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     if let cell = tableView.dequeueReusableCell(withIdentifier: "CategoryCell") as? CategoryCell { 
      let category = DataService.instance.getCategories()[indexPath.row] 
      cell.updateViews(category: category) 
      return cell 
     }else{ 
      return CategoryCell() 
     } 
    } 
} 

です。 ハードコードされたデータでは、私のアプリはthisのように見えますが、私はfirebaseのデータで同じ結果を得たいと思います。カテゴリーへの変更があるその配列をいつでも更新するためのviewDidLoadで、その後

var tableData = [Category]()

、firebaseオブザーバを設定します。

答えて

1

ちょうどテーブルビューのデータソースには、カテゴリの配列を使用して、このようにそれを試してみてくださいfirebaseのノード:

ref.child("Category").observe(.value, with: { snapshot in 
    var newTableData: [Category] = [] 

    for category in snapshot.children { 

     let dict = category.value as! [String: AnyObject] 

     let title  = dict["title"] as! String 
     let imageName = dict["imageName"] as! String 

     let newCategory = Category(title: title, 
           imageName: imageName) 

     newTableData.append(newCategory) 
    } 

    self.tableData = newTableData 
    self.tableview.reloadData() 
}) 
+0

@meggarさんのお返事ありがとうございます。しかし、このコードを置く場所と何をすべきか理解できません。 :( –

関連する問題