我正在努力了解Firebase实时数据库,并使其与餐厅订购应用程序中的Table View完美配合!
在阅读我的代码之前(请不要笑),我希望TableView能够做到的是
1)按类别对请求进行排序(使用从键“ RequestItemCategory”检索的值,该键的Int值为1、2、3) 2)将新请求添加到表视图中,然后根据类别再次对请求进行排序 3)当客户或员工在另一台设备上勾选“完成订单”时,将从表格视图中删除完成的食物请求,这将触发将Firebase上被请求孩子的“ RequestCompleted”键更改为“ true”)
经过数小时的摸索,我唯一能实现的是
我尝试过的 1)标记.queryEqual(toValue:“ false”,childKey:“ RequestCompleted”)尝试滤除已完成的请求,返回一个空表(不是nil信息) 2)我可以使用按钮清除requestArray,然后再次调用retrieveRequest()。这将刷新表,并且所有请求的顺序都正确,但我希望尽可能自动完成。另外,此方法将创建另一个观察者,因此我结束了添加到列表的多个相同订单... 3)使用.value而不是.childAdded-返回nil并且应用程序崩溃-我认为我需要使用for循环,但不确定如何做到这一点。
请帮助谢谢!
代码为:
类对象
class Request {
var name : String = ""
var requestItem : String = ""
var seat : String = ""
var requestCategory : String = ""
}
Cell.swift
import UIKit
class CustomCellTableViewCell: UITableViewCell {
@IBOutlet var requestText : UITextField!
@IBOutlet var nameText: UITextField!
@IBOutlet var seatNumberText: UITextField!
@IBOutlet var timerText: UITextField!
@IBOutlet var cellBackground : UIView!
@IBOutlet var cellCategoryView : UIView!
@IBOutlet var cellCategoryLabel : UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
ViewController.swift
import UIKit
import Firebase
import SVProgressHUD
class MainPageViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate {
@IBOutlet weak var requestTableView: UITableView!
var requestArray : [Request] = [Request]()
override func viewDidLoad() {
super.viewDidLoad()
requestTableView.delegate = self
requestTableView.dataSource = self
requestTableView.register(UINib(nibName: "CustomCellTableViewCell", bundle: nil), forCellReuseIdentifier: "customRequestCell")
configureTableView()
retrieveRequest()
// Do any additional setup after loading the view.
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "customRequestCell", for: indexPath) as! CustomCellTableViewCell
cell.nameText.text = requestArray[indexPath.row].name
cell.seatNumberText.text = requestArray[indexPath.row].seat
cell.requestText.text = requestArray[indexPath.row].requestItem
cell.cellCategoryLabel.text = requestArray [indexPath.row].requestCategory
if cell.cellCategoryLabel.text == "1" {
cell.cellBackground.backgroundColor = UIColor.init(red: 142/255, green: 0/255, blue: 0/255, alpha: 0.65)
} else if cell.cellCategoryLabel.text == "2" {
cell.cellBackground.backgroundColor = UIColor.init(red: 234/255, green: 179/255, blue: 0/255, alpha: 0.75)
} else {
cell.cellBackground.backgroundColor = UIColor.init(red: 0/255, green: 113/255, blue: 119/255, alpha: 0.65)
}
return cell
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return requestArray.count
}
func configureTableView() {
requestTableView.rowHeight = UITableView.automaticDimension
requestTableView.estimatedRowHeight = 73
}
func retrieveRequest() {
let ref = Database.database().reference().child(“Restaurant”).child("Requests").queryOrdered(byChild: "RequestItemCategory")**.queryEqual(toValue: "false", childKey: "RequestCompleted")** adding .queryEqual doesn't work...
ref.observe(.childAdded) { (snapshot) in
let snapshotValue = snapshot.value as? [String : AnyObject] ?? [:]
let customerName = snapshotValue["Lastname"]
let customerSeatnumber = snapshotValue[“Seat”]
let customerRequestedItem = snapshotValue["RequestItem"]
let customerRequestedItemCategory = snapshotValue ["RequestItemCategory"]
let request = Request()
request.name = customerName as! String
request.seat = customerSeatnumber as! String
request.requestItem = customerRequestedItem as! String
request.requestCategory = "\(customerRequestedItemCategory!)"
self.requestArray.append(request)
self.configureTableView()
self.requestTableView.reloadData()
}
}
}
ps。 requestLabel.text用于根据类别(RequestItemCategory)更改特定UIView的颜色