我有一个CartItem
结构,用于存储显示在UITableView
中的项目。我的UITableView
位于CartViewController
中。我将custom cell
使用table view
。这是我的custom cell
:
class CartItemCell: UITableViewCell {
@IBOutlet weak var productImage: UIImageView!
@IBOutlet weak var productName: UILabel!
@IBOutlet weak var productPrice: UILabel!
@IBOutlet weak var productTotalPrice: UILabel!
@IBOutlet weak var stepper: UIStepper!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
var cellIndex: Int?
var product: CartItem?{
didSet{
// change the cell in table view when first
// initialized and when stepper value is changed
.
.
.
}
@IBAction func stepperValueChanged(_ sender: UIStepper) {
CartItem.cartItems[self.cellIndex!].number = Int(sender.value)
self.product?.number = CartItem.cartItems[self.cellIndex!].number
}
}
在CartViewController
中,我有totalPrice UILabel
,它是从CartItem.totalPrice
静态变量中获取其值的。每次单击CartItem.totalPrice
中的UIStepper
时,都会计算和更改UITableViewCell
的值。这是CartViewController
class CartViewController: UIViewController{
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var cartLabel: UILabel!
@IBOutlet weak var totalPrice: UILabel!
var items = CartItem.cartItems
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
tableView.delegate = self
tableView.register(UINib(nibName: "CartItemCell", bundle: nil), forCellReuseIdentifier: "ReusableCell")
self.totalPrice.text = String(Int(CartItem.totalPrice))
tableView.reloadData()
}
}
extension CartViewController: UITableViewDataSource{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return items.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ReusableCell", for: indexPath) as! CartItemCell
cell.product = items[indexPath.item]
cell.cellIndex = indexPath.item
return cell
}
}
totalPrice
时都要更改CartViewController
中的CartItem.totalPrice
标签。我在CartItem.totalPrice
CartItemCell
的值
答案 0 :(得分:1)
使用委托或闭包将值从tableviewcell更新到viewController
return ptr[location]
然后在cellforRow中设置委托
Protocol CartItemProtocol: AnyObject{
func valueUpdated()
}
class CartItemCell: UITableViewCell {
@IBOutlet weak var productImage: UIImageView!
@IBOutlet weak var productName: UILabel!
@IBOutlet weak var productPrice: UILabel!
@IBOutlet weak var productTotalPrice: UILabel!
@IBOutlet weak var stepper: UIStepper!
weak var delegate: CartItemProtocol?
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
var cellIndex: Int?
var product: CartItem?{
didSet{
// change the cell in table view when first
// initialized and when stepper value is changed
.
.
.
}
@IBAction func stepperValueChanged(_ sender: UIStepper) {
CartItem.cartItems[self.cellIndex!].number = Int(sender.value)
self.product?.number = CartItem.cartItems[self.cellIndex!].number
self.delegate.valueUpdated()
}
}
并以委托人身份更新标签
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) ->
UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ReusableCell", for: indexPath) as! CartItemCell
cell.product = items[indexPath.item]
cell.cellIndex = indexPath.item
cell.delegate = self
return cell
}