gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 04:39:25 25 4
gpt4 key购买 nike

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

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

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

enter image description here

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]()
}

最佳答案

只需创建一个协议(protocol)来创建委托(delegate),并在单元初始化时将其分配给您的购物车类

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

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

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

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

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

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

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
}

关于ios - 如何将单元格中单击的按钮上的数据传递到另一个表格 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58649979/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com