如何解决如何通过点击单元格将数据添加到 Firestore Swift
我有更多的餐馆,每家都有不同的食物。Look here 这就是我从 Firestore 检索数据的方式。在前面的控制器中,我有一个餐馆列表,每个包含一个食物列表。
struct Food {
var photoKeyRestaurant: String
var foodName: String
var foodDescription: String
var restaurantName: String
var priceFood: Int
}
class RestaurantViewController: UIViewController {
var restaurantName: String!
var food: [Food] = []
private let tableView: UITableView = {
let table = UITableView()
return table
}()
func getDatabaseRecords() {
let db = Firestore.firestore()
// Empty the array
food = []
db.collection("RestaurantViewController").whereField("restaurantName",isEqualTo: restaurantName).getDocuments { (snapshot,error) in
if let error = error {
print(error)
return
} else {
for document in snapshot!.documents {
let data = document.data()
let newEntry = Food(photoKeyRestaurant: data["photoKeyRestaurant"] as! String,foodName: data["foodName"] as! String,foodDescription: data["foodDescription"] as! String,restaurantName: data["restaurantName"] as! String,priceFood: data["priceLabel"] as! Int
)
self.food.append(newEntry)
}
}
dispatchQueue.main.async {
// self.datas = self.filteredData
self.tableView.reloadData()
}
}
}
如何通过在此功能中按 + 将所选单元格的数据添加到 Firestore? 我在我的 FoodTableViewCell 中创建了一个协议,我在 RestaurantViewController 中调用了它。
func diddTapButtonCell(_ cell: FoodTableViewCell) {
let db = Firestore.firestore()
db.collection("cart").addDocument.(data: foodName) { (err) in
}
编辑:添加表格视图
func tableView(_ tableView: UITableView,numberOfRowsInSection section: Int) -> Int {
return food.count
}
func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "FoodTableViewCell",for: indexPath) as! FoodTableViewCell
cell.delegate = self
let mancare = food[indexPath.row]
let storageRef = Storage.storage().reference()
let photoRef = storageRef.child(mancare.photoKeyRestaurant)
cell.foodImage.sd_setimage(with: photoRef)
cell.descriptionLabel.text = mancare.foodDescription
cell.foodNameLabel.text = mancare.foodName
cell.priceLabel.text = "\(mancare.priceFood) lei"
//Fac ca imaginea sa fie cerc - start
cell.foodImage.layer.borderWidth = 1
cell.foodImage.layer.masksToBounds = false
cell.foodImage.layer.borderColor = UIColor.black.cgColor
cell.foodImage.layer.cornerRadius = cell.foodImage.frame.height/2
cell.foodImage.clipsToBounds = true
//Fac ca imaginea sa fie cerc - finish
return cell
}
这是我的tableview单元格代码
protocol CustomCellDelegate {
func diddTapButtonCell (_ cell: FoodTableViewCell)
}
class FoodTableViewCell: UITableViewCell {
var delegate: CustomCellDelegate?
@IBOutlet weak var foodImage: UIImageView!
@IBOutlet weak var foodNameLabel: UILabel!
@IBOutlet weak var descriptionLabel: UILabel!
@IBOutlet weak var priceLabel: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
}
override func setSelected(_ selected: Bool,animated: Bool) {
super.setSelected(selected,animated: animated)
}
@IBAction func addToCart(_ sender: Any) {
delegate?.diddTapButtonCell(self)
}
}
解决方法
https://firebase.google.com/docs/firestore/manage-data/add-data
这是我的一个项目中的一个示例,用于将数据添加到 Firestore。
func updateDocument(rootCollection : String,doc: String,newValueDict: [String : Any],completion:@escaping (Bool) -> Void = {_ in }) {
let db = Firestore.firestore()
db.collection(rootCollection).document(doc).setData(newValueDict,merge: true){ err in
if let err = err {
print("Error writing document: \(err)")
completion(false)
}else{
completion(true)
}
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。