【发布时间】:2017-05-17 11:23:18
【问题描述】:
我正在创建一个 iOS 应用程序,它使用每个地点的地名和评级。我已经让这件事发挥作用了。我的意思是,我将数据保存到我的数据库中,并且我也可以读取它们。唯一的问题是,当我阅读它们时,我希望它们通过计算每个位置的平均值来加载到我的 tableviewcell 上。查看屏幕截图,如果您有不明白的地方,请让我编辑答案。
表格视图
Firebase
将数据加载到 tableview 的我的代码
import UIKit
import FirebaseDatabase
class PlacesTableViewController: UITableViewController {
//MARK: Properties
@IBOutlet weak var placesTableView: UITableView!
var dbRef:FIRDatabaseReference?
var places = [Places]()
private var loadedLabels = [String: String]()
private var loadedRatings = [String: Int]()
override func viewDidLoad()
{
super.viewDidLoad()
dbRef = FIRDatabase.database().reference()
// Loads data to cell.
loadData()
}
override func numberOfSections(in tableView: UITableView) -> Int
{
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
//return the number of rows
return places.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
// Table view cells are reused and should be dequeued using a cell identifier.
guard let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as? PlacesTableViewCell else {
fatalError("The dequeued cell is not an instance of PlacesTableView Cell.")
}
let place = places[indexPath.row]
cell.placeLabel.text = place.name
cell.ratingControl.rating = place.rating
return cell
}
private func loadData()
{
dbRef!.observe(.childAdded, with: {
(placeSnapshot) in
//print("Adding place \(placeSnapshot.key)...")
let labels = placeSnapshot.childSnapshot(forPath: "placeLabel")
for (key, label) in labels.value as! [String: String] {
self.updatePlace(key, label: label)
}
let ratings = placeSnapshot.childSnapshot(forPath: "rating")
for (key, rating) in ratings.value as! [String: Int] {
self.updatePlace(key, rating: rating)
}
})
}
private func updatePlace(_ key: String, label: String? = nil, rating: Int? = nil)
{
if let label = label {
loadedLabels[key] = label
}
if let rating = rating {
loadedRatings[key] = rating
}
guard let label = loadedLabels[key], let rating = loadedRatings[key] else {
return
}
if let place = Places(name: label, rating: rating) {
places.append(place)
placesTableView.reloadData()
}
}
}
快速定位
import UIKit
class Places {
//MARK: Properties
var name: String
var rating: Int
//MARK:Types
struct PropertyKey {
static let name = "name"
static let rating = "rating"
}
//MARK: Initialization
init?(name: String, rating: Int) {
// Initialize stored properties.
self.name = name
self.rating = rating
// Initialization should fail if there is no name or if the rating is negative.
// The name must not be empty
guard !name.isEmpty else {
return nil
}
// The rating must be between 0 and 5 inclusively
guard (rating >= 0) && (rating <= 5) else {
return nil
}
}
}
【问题讨论】:
-
不清楚你在问什么。您是否正在尝试获得某个地方的平均评分?如果是这样,那就是读取每个地方的评分节点并平均子节点的值;(2 + 4 + 5)/ 3?
-
有错误信息吗?是否有一些代码不能按预期的方式工作?与其向我们展示你所有的代码,不如选择一件阻碍你的具体事情并描述它。例如,不了解火力基地的人可能会提供帮助。缩小问题范围。
-
@Jay 是的,这就是我想要的。
-
@Mozahler 在另一个函数中出现错误,而不是读取值。当用户添加新地点时,tableview 尝试更新并崩溃,但如果他重新打开应用程序,则“新地点”会正确显示在 tableview 上。这是我的previous 问题中的一个问题。
-
那么是您的代码崩溃的问题,还是您需要知道如何读取节点并找到一系列数字的平均值的问题?
标签: ios swift uitableview firebase firebase-realtime-database