我正在尝试将数据从 ViewController 传递到我的 CartViewController。 ViewController 中的单元格有 3 个按钮(optionBtns),每个按钮上方都有价格和重量标签。
我想做的是让选择的 optionBtn 在按下 ATC 按钮后传递其上方的标签数据
单元格中的ATC按钮将图像、名称、类别和optionBtn数据传递给CartViewController单元格(CartCell)
当按下 ATC 以在具有选定 optionBtn 数据(价格和重量)的单元格中显示选定项目名称、图像和类别时,我如何能够将选定数据传递给 CartVC
我还使用 Cloud Firestore 发布数据来填充我的 VC 单元
class Cell: UITableViewCell {
weak var items: Items!
@IBOutlet weak var name: UILabel!
@IBOutlet weak var category: UILabel!
@IBOutlet weak var productImage: UIImageView!
@IBOutlet weak var weightOne: UILabel!
@IBOutlet weak var weightTwo: UILabel!
@IBOutlet weak var weightThree: UILabel!
@IBOutlet weak var priceOne: UILabel!
@IBOutlet weak var priceTwo: UILabel!
@IBOutlet weak var priceThree: UILabel!
@IBOutlet weak var addToCart: RoundButton!
@IBOutlet weak var optionBtn1: RoundButton!
@IBOutlet weak var optionBtn2: RoundButton!
@IBOutlet weak var optionBtn3: RoundButton!
var addActionHandler: (() -> Void)?
func configure(withItems items: Items) {
name.text = items.name
category.text = items.category
image.sd_setImage(with: URL(string: items.image))
priceOne.text = items.price1
priceTwo.text = items.price2
priceThree.text = items.price3
weightOne.text = items.weight1
weightTwo.text = items.weight2
weightThree.text = items.weight3
self.items = items
}
var lastSelectedButton = UIButton()
@IBAction func cartTypeSelected(_ sender: RoundButton) {
lastSelectedButton.isSelected = false; do {
self.lastSelectedButton.backgroundColor = UIColor.blue
lastSelectedButton = sender
sender.isSelected = true; do {
self.lastSelectedButton.backgroundColor = UIColor.lightGreen
}
}
@IBAction func atcBtn(_ sender: UIButton) {
self.addActionHandler?()
}
}
class CartViewController: UIViewController {
var items: Items!
@IBOutlet weak var cartTableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
cartTableView.dataSource = self
cartTableView.delegate = self
}
}
extension CartViewController: UITableViewDataSource, UITableViewDelegate {
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return Cart.currentCart.cartItems.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CartCell", for: indexPath) as! CartCell
let cart = Tray.currentCart.cartItems[indexPath.row]
cell.configure(withItems: cart)
return cell
}
}
class CartCell: UITableViewCell {
var selctedBtn: Cell?
@IBOutlet weak var lblMealName: UILabel!
@IBOutlet weak var imageUrl: UIImageView!
@IBOutlet weak var lblSubTotal: UILabel!
@IBOutlet weak var lblWeight: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
func configure(withItems items: Items) {
// lblWeight.text = "\(items.weight1)"
lblMealName.text = "\(items.category): \(items.name)"
let formatter = NumberFormatter()
formatter.maximumFractionDigits = 2
formatter.numberStyle = .decimal
// lblSubTotal.text = "$\(formatter.string(for: items.price1)!)"
imageUrl.sd_setImage(with: URL(string: items.imageUrl))
if selctedBtn?.optionBtn1.isSelected == true {
lblSubTotal.text = "$\(formatter.string(for: items.price1)!)"
lblWeight.text = "\(items.weight1)"
} else if selctedBtn?.optionBtn2.isSelected == true {
lblSubTotal.text = "$\(formatter.string(for: items.price2)!)"
lblWeight.text = "\(items.weight2)"
} else if selctedBtn?.optionBtn3.isSelected == true {
lblSubTotal.text = "$\(formatter.string(for: items.price3)!)"
lblWeight.text = "\(items.weight3)"
}
}
}
class Cart {
static let currentCart = Cart()
var cartItems = [Items]()
}
最佳答案
如果想法是让 firebase 处理您的所有数据,那么数据应该通过 firebase 而不是通过 viewController。即你的 Firebase 数据库应该有一个 items
集合(或者每个 store
一个)并且你的 user
应该有一个 cart
收藏。当用户点击添加到购物车按钮时,您将有问题的项目添加到 firebase 中的用户购物车集合,然后显示 cartViewController。 cartViewController 应该订阅当前用户的 cart
集合,然后从该 firebase 集合填充它的 tableview。
TLDR firebase 应用程序的典型设计是 firebase 数据库是应用程序的真实来源,因此您应该将所有更改写入 firebase,然后只需在应用程序的其他地方观察这些集合。这也确保了如果用户在另一台设备上编辑购物车,它将在 iOS 设备上使用新的购物车项目自行更新。
关于ios - 如何在单元格中按下按钮时将数据从一个 Tableview 传递到另一个 Tableview?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58635041/