我有一个类似的FirebaseStructure:
"users" : { "OpeHS4UhH6YWcK4aPHJWumueCQP2" : {
"Orders" : {
"Date" : {
"-LQYDWAKlzTrlTtO1Qiz" : 1541410526186,
"-LQYspEghK3KE27MlFNE" : 1541421618601,
"-LQsILjpNqKwLl9XBcQm" : 1541764115618
},
"Order" : {
"-LQYDWAKlzTrlTtO1Qiy" : "1 Tomato, 3 Orange, 2 Banana",
"-LQYspEfZaJIt-PaAvYa" : "1 Banana, 9 Apple, 2 Tomato",
"-LQsILjpNqKwLl9XBcQl" : "1 Apple"
}
}
}
我想在tableView
中显示订单历史记录
如何在没有tableView
的情况下用"Order"
填充childByAutoID
?
我已经尝试过了,但是它是空的:
class OrderHistoryTableViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var orderDateHistoryArray = [Double]()
var orderItemsHistoryArray = [String]()
@IBOutlet var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
getOrderHistory()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return orderItemsHistoryArray.count
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 100
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "OrderHistoryCell") as! OrderHistoryTableViewCell
cell.orderHistoryItemsLabel.text = orderItemsHistoryArray[indexPath.row]
return cell
}
func getOrderHistory() {
//Get userinfo from database
let uid = Auth.auth().currentUser!.uid
let orderDateHistoryRef = Database.database().reference().child("users/\(uid)/Orders/")
orderDateHistoryRef.observeSingleEvent(of: .value, with: { (snapshot) in
// Get user value
let value = snapshot.value as? NSDictionary
let orderItem = value?["Order"] as? String ?? ""
self.orderItemsHistoryArray += [orderItem]
print(snapshot)
print(orderItem)
self.tableView.reloadData()
// ...
}) { (error) in
print(error.localizedDescription)
}
}
}
答案 0 :(得分:1)
Order
是Dictionary
而不是String
if let orderItem = value?["Order"] as? [String:String] {
self.orderItemsHistoryArray += Array(orderItem.values)
}