簡體   English   中英

如何從Firebase檢索數據到TableViewCell

[英]How to retrieve data from Firebase to TableViewCell

我正在研究簡單的程序-創建產品對象,然后計算它們的卡路里。

我想在TableViewController中獲得所有產品

我創建了一個方法,可以將數據正確保存在Firebase中,但是在將它們檢索到單元格時卡住了。 我沒有任何錯誤,但是我也沒有任何結果

import UIKit
import Firebase

class MainTableViewController: UITableViewController {

    var products = [Products]()


    override func viewDidLoad() {
        super.viewDidLoad()

            DataService.dataService.PRODUCT_BASE.observeEventType(.Value, withBlock: { snapshot in

                print(snapshot.value)

                self.products = []

                if let snapshots = snapshot.children.allObjects as? [FIRDataSnapshot] {

                    for snap in snapshots {

                        if let postDictionary = snap.value as? Dictionary<String, AnyObject> {

                            let key = snap.key

                            let product = Products(key: key, dictionary: postDictionary)


                        }
                    }
                }

                self.tableView.reloadData()
        })


    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: - Table view data source

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return products.count
    }


    override func tableView(tableView:UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {


        let cell = tableView.dequeueReusableCellWithIdentifier("ProductCell") as! UITableViewCell?
        let ProductItem = products[indexPath.row]

        cell?.textLabel?.text = ProductItem.productName
        cell?.detailTextLabel?.text = String(ProductItem.productCalories)

        return cell!

    }

這是我的DataService文件:

import Foundation
import Firebase

let URL_BASE = FIRDatabase.database().reference()

class DataService {

    static let dataService = DataService()

    private var _REF_BASE = URL_BASE
    private var _USER_BASE = URL_BASE.child("users")
    private var _PRODUCЕ_BASE = URL_BASE.child("products")

    var REF_BASE: FIRDatabaseReference {
        return _REF_BASE
    }

    var USER_BASE: FIRDatabaseReference {
        return _USER_BASE
    }

    var PRODUCT_BASE: FIRDatabaseReference {
        return _PRODUCЕ_BASE
    }


    var CURRENT_USER_REF: FIRDatabaseReference {

        let userID = NSUserDefaults.standardUserDefaults().valueForKey("uid") as! String

        let currentUser = URL_BASE.child("users").child(userID)

        return currentUser

    }


    func CreateNewProduct(product: Dictionary<String, AnyObject>) {

        let ProductNewRef = DataService.dataService.PRODUCT_BASE.childByAutoId()

        ProductNewRef.setValue(product)

    }

我無法理解我做錯了什么。 所有產品均以字典形式表示。

import Foundation
import Firebase

class Products {

    private var _productName: String!

    private var _productCalories: Int!


    var productName: String {

        return _productName

    }

    var productCalories: Int {

        return _productCalories
    }

    init(key: String, dictionary: Dictionary<String, AnyObject>) {


        if let calories = dictionary["calories"] as? Int {

            self._productCalories = calories
        }

        if let name = dictionary["name"] as? String {

            self._productName = name
        }


    }


}

您利用了

var products = [Products]()

但不從Firebase回調中添加任何項目

if let postDictionary = snap.value as? Dictionary<String, AnyObject> {  
         let key = snap.key 
         let product = Products(key: key, dictionary: postDictionary)
 }

請添加self.products.append(product)

if let postDictionary = snap.value as? Dictionary<String, AnyObject> {  
         let key = snap.key 
         let product = Products(key: key, dictionary: postDictionary)
         self.products.append(product)
 }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM