我是编程新手,想尝试一些事情。所以我有两个视图控制器。第一个有一个按钮,点击时将我带到第二个视图控制器。在第二个视图控制器中,我有一个带有原型单元格的表格视图。在我的类 tableViewCell 中,我定义了这个单元格,还有两个函数用于 - 按钮和 + 按钮。所以问题是,这些函数工作并像步进器一样在单元格的标签中设置值。但是当我回到我的第一个视图控制器,然后再次进入第二个视图控制器时,标签的值又是 1。那么我怎样才能保存这个标签的点击值呢?您可以在此处查看我的单元格的代码:
import UIKit
import CoreData
protocol FridgeListTableViewCellDelegate: class {
func cellDecreaseButtontapped(_ sender: FridgeListTableViewCell)
func cellIncreasedButtontapped(_ sender: FridgeListTableViewCell)
}
class FridgeListTableViewCell: UITableViewCell {
@IBOutlet weak var wareLabel: UILabel!
@IBOutlet weak var quantityLabel: UILabel!
@IBOutlet weak var decreaseButton: UIButton!
@IBOutlet weak var increaseButton: UIButton!
weak var delegate: FridgeListTableViewCellDelegate?
@IBAction func decreaseTapped(_ sender: UIButton) {
let counter = Int(quantityLabel.text!)
let newCounter = counter! - 1
let newCounterString = String(newCounter)
quantityLabel.text = newCounterString
delegate?.cellDecreaseButtontapped(self)
}
@IBAction func increaseTapped(_ sender: UIButton) {
let counter = Int(quantityLabel.text!)
let newCounter = counter! + 1
let newCounterString = String(newCounter)
quantityLabel.text = newCounterString
delegate?.cellIncreasedButtontapped(self)
}
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
和我的第二个视图控制器的一部分,我称之为函数:
func cellDecreaseButtontapped(_ sender: FridgeListTableViewCell) {
guard let tappedIndexPath = fridgeList.indexPath(for: sender) else { return }
print("Decrease", sender, tappedIndexPath)
}
func cellIncreasedButtontapped(_ sender: FridgeListTableViewCell) {
guard let tappedIndexPath = fridgeList.indexPath(for: sender) else { return }
print("Increase", sender, tappedIndexPath)
}
希望有人可以提供帮助。