gpt4 book ai didi

ios - 如何从 Firebase 检索数据到 UITableViewCell

转载 作者:行者123 更新时间:2023-11-30 13:18:25 25 4
gpt4 key购买 nike

我正在开发简单的程序 - 我创建产品对象,然后计算它们的卡路里。

我想在 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)
}

关于ios - 如何从 Firebase 检索数据到 UITableViewCell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38011403/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com