繁体   English   中英

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

[英]How to pass data on button clicked in cell to another tableview?

第一:我如何能够将数据从 ItemVC 单元格传递到所选单元格中单击的按钮(添加到购物车按钮(ATC))上的 CartVC,因为我没有尝试使用 didSelectRowAt 将数据传递给 CartVC。 但 ATC btn 将单元格数据传递给 CartVC

我从 ItemVC 到 CartVC 的 Segue 在 BarButtonItem(cartBtn) 中,所以我不想在按下 ATC 按钮时跳转到 CartVc,但只在按下 ATC 时将所选项目数据传递给 CartVC

其次,当按下 ATC 时,我如何能够将 lblQty 中的增量/减量值传递给 CartVC,以便能够呈现更准确的小计

在此处输入图像描述

import UIKit
import SDWebImage
import Firebase


class ItemCell: 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 lblQty: UILabel!

    @IBOutlet weak var addToCart: RoundButton!
    @IBOutlet weak var plusBtn: RoundButton!
    @IBOutlet weak var minusBtn: RoundButton!

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

}

import UIKit
import Firebase
import FirebaseFirestore

class ItemViewController: UITableViewController {

    @IBOutlet weak var cartBtn: BarButtonItem!!
    @IBOutlet weak var tableView: UITableView!

    var itemSetup: [Items] = []

    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.delegate = self

        fetchItems { (items) in
            self.itemSetup = items.sorted
            self.tableView.reloadData()
        }

    }
    func fetchItems(_ completion: @escaping ([Items]) -> Void) {
       // -** FireStore Code **-
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        let vc = segue.destination as? CartViewController {
            vc.items = self.itemSetup
        }
    }
    @objc func plusItem(_ sender: UIButton) {
        // -** increase Qty Code **-
    }

    //Function to decrement item count
    @objc func minusItem(_ sender: UIButton) {
      //  -** decrease Qty Code **-
    }

    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: "ItemCell") as? ItemCell else { return UITableViewCell() }

        cell.configure(withItem: itemSetup[indexPath.row])

        cell.lblQty.text = "\(self.imageSetup[indexPath.row].count)"
        cell.plusBtn.tag = indexPath.row
        cell.plusBtn.addTarget(self, action: #selector(self.plusItem(_:)), for: .touchUpInside)
        cell.minusBtn.tag = indexPath.row
        cell.minusBtn.addTarget(self, action: #selector(self.minusItem(_:)), for: .touchUpInside)

        return cell
    }

}

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 = Cart.currentCart.CartItems[indexPath.row]
        cell.qty.text = "\(cart.qty)"
        cell.lblMealName.text = "\(cart.items.category): \(cart.items.name)"
        cell.lblSubTotal.text = "$\(cart.items.price1 * Float(cart.qty))"

        cell.imageUrl                             // can't figure out how to pass image

        return cell
    }
}

import Foundation

class CartItem {

    var items: Items
    var qty: Int

    init(items: Items, qty: Int) {
        self.items = items
        self.qty = qty
    }
}

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

只需创建一个协议来创建一个委托并在初始化单元格时将其分配给您的购物车 class

protocol ItemCellDelegate {
    func itemCell(didTapButton button: UIButton)
}    

然后在 ItemCell 中有一个委托属性

class ItemCell {
    weak var delegate: ItemCellDelegate?
    ...
    // Then call the delegate method when the buttons is tapped.
    func buttonTapped() {
        delegate?.itemCell(didTapButton: theCellsButton)
    }
}

然后使您的购物车符合委托

extension Cart: ItemCellDelegate {
    func itemCell(didTapButton button: UIButton) {
        // Do you stuff with button, or any other data you want to pass in
    }
}

然后在返回单元格之前设置委托。

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.qty.text = "\(cart.qty)"
    cell.lblMealName.text = "\(cart.items.category): \(cart.items.name)"
    cell.lblSubTotal.text = "$\(cart.items.price1 * Float(cart.qty))"

    cell.delegate = Cart.currentCart //You should rename this static var to just 'current'

    return cell
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM