【发布时间】:2020-03-05 02:39:45
【问题描述】:
我的单元格中有三个带有价格和重量标签的按钮,我想做的是让选定的 optionBtn 将重量和价格数据传递给 CartVC
我目前在 CartCell 中的代码尚未发布所选 optionBtn 的重量和价格标签的数据
我在 CartCell 中设置的函数 func configure 用于在 CartVC 的单元格中呈现数据
当按下 atcBtn 将数据传递给 CartVC 时,购物车会显示名称、类别和图像
我想要的是显示Cartvc细胞中所选的选项BBTN价格和重量(选择时)我将如何修改我为Func中为选项所设置的代码进行修改
import UIKit
import SDWebImage
import Firebase
class Cell: UITableViewCell {
weak var items: Items!
@IBOutlet weak var name: UILabel!
@IBOutlet weak var category: UILabel!
@IBOutlet weak var productImage: UIImageView!
@IBOutlet weak var weightOne: UILabel!
@IBOutlet weak var weightTwo: UILabel!
@IBOutlet weak var weightThree: UILabel!
@IBOutlet weak var priceOne: UILabel!
@IBOutlet weak var priceTwo: UILabel!
@IBOutlet weak var priceThree: UILabel!
@IBOutlet weak var addToCart: RoundButton!
@IBOutlet weak var optionBtn1: RoundButton!
@IBOutlet weak var optionBtn2: RoundButton!
@IBOutlet weak var optionBtn3: 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))
priceOne.text = items.price1
priceTwo.text = items.price2
priceThree.text = items.price3
weightOne.text = items.weight1
weightTwo.text = items.weight2
weightThree.text = items.weight3
}
@IBAction func atcBtn(_ sender: UIButton) {
self.addActionHandler?()
}
}
import UIKit
import Firebase
import FirebaseFirestore
class ViewController: 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: "Cell") as? Cell else { return UITableViewCell() }
let item = itemSetup[indexPath.row]
cell.configure(withItem: item)
cell.addActionHandler = {
Cart.currentCart.items.append(item)
}
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 = Tray.currentCart.cartItems[indexPath.row]
cell.configure(withItems: cart)
return cell
}
}
class CartCell: UITableViewCell {
var selctedBtn: Cell?
@IBOutlet weak var lblMealName: UILabel!
@IBOutlet weak var imageUrl: UIImageView!
@IBOutlet weak var lblSubTotal: UILabel!
@IBOutlet weak var lblWeight: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
var lastSelectedButton = UIButton()
func configure(withItems items: Items) {
// lblWeight.text = "\(items.weight1)"
// lblSubTotal.text = "$\(formatter.string(for: items.price1)!)"
lblMealName.text = "\(items.category): \(items.name)"
let formatter = NumberFormatter()
formatter.maximumFractionDigits = 2
formatter.numberStyle = .decimal
imageUrl.sd_setImage(with: URL(string: items.imageUrl))
// optionBtns I dont know how to set the code to where I can individual
// select a btn to pass the data to the cell
if selctedBtn?.optionBtn1.isSelected == true {
lblSubTotal.text = "$\(formatter.string(for: items.price1)!)"
lblWeight.text = "\(items.weight1)"
} else if selctedBtn?.optionBtn2.isSelected == true {
lblSubTotal.text = "$\(formatter.string(for: items.price2)!)"
lblWeight.text = "\(items.weight2)"
} else if selctedBtn?.optionBtn3.isSelected == true {
lblSubTotal.text = "$\(formatter.string(for: items.price3)!)"
lblWeight.text = "\(items.weight3)"
}
// or this
switch lastSelectedButton {
case selctedBtn!.optionBtn1:
isSelected = true
lblSubTotal.text = "$\(formatter.string(for: items.price1)!)"
lblWeight.text = "\(items.weight1)"
case selctedBtn!.optionBtn2:
isSelected = true
lblSubTotal.text = "$\(formatter.string(for: items.price2)!)"
lblWeight.text = "\(items.weight2)"
case selctedBtn!.optionBtn3:
isSelected = true
lblSubTotal.text = "$\(formatter.string(for: items.price3)!)"
lblWeight.text = "\(items.weight3)"
default:
break
}
}
// still running tests to make this work just can't seem to have the selected buttons data pass to the Cart Cells
}
更新:
刚刚添加了一些我一直在测试的代码,在选择选项按钮后如何将标签传递到购物车方面仍然没有运气
【问题讨论】:
标签: ios swift uitableview if-statement uibutton