如何通过点击单元格将数据添加到Firestore Swift中
我有更多的餐厅,每一家都有不同的食物。 这就是我从Firestore检索数据的方式。在前面的控制器中,我有一个餐厅列表,每个餐厅都包含一个食物列表如何通过点击单元格将数据添加到Firestore Swift中,swift,firebase,uitableview,google-cloud-firestore,Swift,Firebase,Uitableview,Google Cloud Firestore,我有更多的餐厅,每一家都有不同的食物。 这就是我从Firestore检索数据的方式。在前面的控制器中,我有一个餐厅列表,每个餐厅都包含一个食物列表 struct Food { var photoKeyRestaurant: String var foodName: String var foodDescription: String var restaurantName: String var priceFood: Int } cla
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)
}
}
下面是我的一个项目中向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)
}
}
}
请用UITableView的代码编辑您的问题。@Todd check Out当您按下+按钮时,diddTapButtonCell是否已添加为目标?如果通过故事板/界面生成器连接,我看不到“objc”或“iAction”。此外,如果您能够按单元格上的任意位置,我将改为在下面实现didSelectRowAt UITableViewDelegate方法cellForRowAt@landnbloc我编辑了这个问题。查看TableView单元格将didTapButtonCell转换为一个闭包“var didTapButton:(()->())?”,然后在TableView的cellForAt函数“cell.didTapButton={//从此处直接处理单元格操作}中为其赋值,并确保添加didTapButton?()要添加到Cart。这应该可以。感谢您与我共享这些,在newValueDict中,我如何从当前单元格传递值?您实际上不想从单元格传递数据,因为单元格是重复使用的,因此无法在tableview滚动时获得正确的数据。这就是“cell.didTapButton={self?.updateDocument”的原因(newDataDict:[“foodDesc”:mancare.foodDescription])}也是个好主意。这样您就可以直接从数据源添加数据