【问题标题】:passing data from button in cell to another tableview?将数据从单元格中的按钮传递到另一个表格视图?
【发布时间】:2020-03-03 11:13:06
【问题描述】:

如何将数据从菜单表视图中的按钮传递到购物车表视图? 我会继续它,使用闭包,协议/委托,还是别的什么?

我无法将数据从 MenuViewController 中的 AddtoCart Button 传递到 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
    }
}

【问题讨论】:

  • 您没有显示当用户点击添加到购物车按钮时运行的代码,但它应该将商品添加到购物车。展示购物车 VC 应该简单地展示购物车。应该不需要添加到购物车按钮直接与展示车 VC 通信。
  • @Paulw11,你说得对,我删除了之前尝试使用的代码,因为它无法从 ATC 传递数据在 MenuVC 单元格中的 btn 到 CartVC。因为我尝试的大多数事情都不起作用或者只是没有将数据推送到 CartVC 的数据

标签: ios swift uitableview google-cloud-firestore segue


【解决方案1】:

我要做的第一件事是摆脱CartItem - 除了包装Items 实例之外,它似乎没有做任何事情,而且您的代码对您是否使用@987654323 感到有些困惑@ 或 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
}

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

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

【讨论】:

  • 我一下课就不能解决这个问题,抱歉,如果您需要一些时间才能得到答复
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-09-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-21
相关资源
最近更新 更多