[英]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.