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

如何将数据从菜单表视图中的按钮传递到购物车表视图? 我会选择使用闭包,协议/代理,还是其他?

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

目标是当在MenuCell中按下ATC按钮时将项目放入CartVC

在按下MenuVC时,NavBar上的CartButton会紧靠CartVC

单元格中的ATC按钮会将所有选定的单元格数据传递到cartVC(图像,名称,类别,重量和价格)

我正在使用Cloud Firestore发布数据以填充我的VC单元

我尝试了很多不同的解决方案,但是似乎仍然没有任何效果,我已经坚持了将近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
    }
}
yegenting2 回答:将数据从单元格中的按钮传递到另一个表视图?

我要做的第一件事就是摆脱ssh://git@192.168.1.245:2224/mark/repo.git -除了包装CartItem实例外,它似乎没有做任何事情,并且代码中是否存在一些混淆正在使用ItemsCartItem(我可能还会将Items重命名为Items-单数)。

Item

要从您的单元中获取“添加到购物车”操作,可以使用委派模式或提供闭包来处理该操作。我将使用闭包

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

现在,在菜单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 } 函数来增加购物车数据模型的功能,如果商品在购物车中,则该数量会增加

本文链接:https://www.f2er.com/3148698.html

大家都在问