将数据从单元格中的按钮传递到另一个表视图?

passing data from button in cell to another tableview?

如何将数据从菜单表视图中的按钮传递到购物车表视图? 我会继续它,使用闭包,protocol/delegates,其他东西吗?

我无法将数据从 MenuViewController 中的 AddtoCart 按钮传递到 CartViewController

objective 是在 MenuCell

中按下 ATC 按钮时将物品放入购物车VC

菜单中 NavBar 上的 CartButtonVC 在按下时转到购物车VC

单元格中的 ATC 按钮将所有选定的单元格数据传递到购物车VC(图片、名称、类别、重量和价格)

我正在使用 Cloud Firestore post 数据来填充我的 VC 单元格

我已经尝试了很多不同的解决方案 posted on stack 但似乎仍然没有任何效果,我已经坚持了将近 2 周...任何帮助将不胜感激

import UIKit
import SDWebImage
import Firebase


class MenuCell: UITableViewCell {

    weak var items: Items!

    @IBOutlet weak var name: UILabel!
    @IBOutlet weak var category: UILabel!
    @IBOutlet weak var productImage: UIImageView!

    @IBOutlet weak var weight: UILabel!
    @IBOutlet weak var price: UILabel!

    @IBOutlet weak var addToCart: RoundButton!

    func configure(withItems items: Items) {
        name.text = items.name
        category.text = items.category
        image.sd_setImage(with: URL(string: items.image))
        price.text = items.price
        weight.text = items.weight
        self.items = items
    }
}

import UIKit
import Firebase
import FirebaseFirestore

class MenuViewController: UITableViewController {

    @IBOutlet weak var cartButton: BarButtonItem!!
    @IBOutlet weak var tableView: UITableView!
    var itemSetup: [Items] = []

    override func viewDidLoad() {
        super.viewDidLoad()

    }

    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return itemSetup.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard let cell = tableView.dequeueReusableCell(withIdentifier: "MenuCell") as? MenuCell else { return UITableViewCell() }
        cell.configure(withItem: itemSetup[indexPath.row])
        return cell
    }
}

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 = Cart.currentCart.CartItems[indexPath.row]
        cell.lblWeight.text = cart.items.weight         
        cell.lblMealName.text = "\(cart.items.category): \(cart.items.name)"
        cell.lblSubTotal.text = "$\(cart.items.price)"  
        cell.imageUrl                                // can't figure out how to pass image

        return cell
    }
}

class CartItem {

    var items: Items

    init(items: Items) {
        self.items = items
    }
}

我要做的第一件事是摆脱 CartItem - 除了包装 Items 实例外,它似乎没有做任何事情,而且您的代码中有些混淆无论您使用的是 CartItem 还是 Items(我可能还会将 Items 重命名为 Item - 单数)。

class Cart {
    static let currentCart = Cart()
    var cartItems = [Items]()
}

要从您的单元格中获取 "add to cart" 操作,您可以使用委托模式或提供闭包来处理该操作。我将使用闭包

class MenuCell: UITableViewCell {

    @IBOutlet weak var name: UILabel!
    @IBOutlet weak var category: UILabel!
    @IBOutlet weak var productImage: UIImageView!

    @IBOutlet weak var weight: UILabel!
    @IBOutlet weak var price: UILabel!

    @IBOutlet weak var addToCart: 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))
        price.text = items.price
        weight.text = items.weight
    }

    @IBAction func addTapped(_ sender: UIButton) {
        self.addActionHandler?()
    }

}

现在,在您的菜单中 cellForRowAt 您可以提供操作处理程序:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "MenuCell") as! MenuCell // Just crash at this point if there isn't a valid cell identifier configured
    let item = itemSetup[indexPath.row]
    cell.configure(withItem: item)
    cell.addActionHandler = {
        Cart.currentCart.items.append(item)
    }
    return cell
}

这就是您需要做的全部 - 当您转到购物车视图控制器时,它将显示购物车的当前内容。

请注意,您可以稍微改进购物车数据模型,方法是允许它为每件商品指定一个数量,并提供一个 add(item:) 函数,如果商品在购物车中则增加数量