如何在单元格中按下按钮时将数据从一个 Tableview 传递到另一个 Tableview?

how to pass data from one Tableview to another Tableview when button is pressed in cell?

我正在尝试将数据从 ViewController 传递到我的购物车ViewController。 ViewController 中的单元格有 3 个按钮 (optionBtns),每个按钮上方都有价格和重量标签。

我想做的是在按下 ATC 按钮后让选择的 optionBtn 传递其上方的标签数据

单元格中的ATC按钮将图像、名称、类别和optionBtn数据的数据传递给购物车ViewController 单元格(CartCell)

当按下 ATC 以在具有选定选项按钮数据(价格和重量)的单元格中显示选定项目名称、图像和类别时,我如何能够将选定数据传递到购物车VC

我也在使用 Cloud Firestore post 数据来填充我的 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 集合填充其表视图。

TLDR firebase 应用程序的典型设计是 firebase 数据库是应用程序的真实来源,因此您应该将所有更改写入 firebase,然后在应用程序的其他地方简单地观察这些集合。这也确保如果用户在另一台设备上编辑购物车,它会在 iOS 设备上使用新的购物车项目自行更新。

您可以在闭包中传递值。

因此,在您的 Cell class 中,您可以将闭包变量更改为:

 var addActionHandler: ((Int) -> Void)?

然后,在您的 addToCart 按钮操作中,执行以下操作:

@IBAction func atcBtn(_ sender: UIButton) {

    // pass back the user selected values
    var i = 0
    switch lastSelectedButton {
    case optionBtn1:
        i = 1
    case optionBtn2:
    i = 2
    default:
        i = 3
    }
    self.addActionHandler?(i)

}

创建项目的 .selectedOption 属性 class,您应该添加一个(Int 类型)。您可以使用它来跟踪用户的选择。

修改 Cell:

中的 cellForAt
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    guard let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as? Cell else { return UITableViewCell() }

    // use var to make item mutable
        var item = itemSetup[indexPath.row]
        cell.configure(withItem: item)
        cell.addActionHandler = { (option: Int) in
        print("Option selected = \(option)")
        item.selectedOption = option

        Cart.currentCart.items.append(item)
    }

    return cell
}

在您的 CartCell 中,您可以制作如下标签:

    if items.selectedOption == 1 {
        lblSubTotal.text = "$\(formatter.string(for: items.price1)!)"
        lblWeight.text = "\(items.weight1)"            

    } else if items.selectedOption == 2 {
        lblSubTotal.text = "$\(formatter.string(for: items.price2)!)"
        lblWeight.text = "\(items.weight2)"

    } else if items.selectedOption == 3 {
        lblSubTotal.text = "$\(formatter.string(for: items.price3)!)"
        lblWeight.text = "\(items.weight3)"

    }