Swift Stepper Action that changes UITextField and

2019-09-11 09:24发布

问题:

I am using a function to build my custom cell. the function used to create the cell is "makeMyList". The stepper I have successfully increments and decrements the itemQuantity textField. The trouble I am having comes with updating the "itemPrice" label. I want itemPrice.text to be quantity * price and update dynamically as the quantity either goes up or down dynamically.

Could someone possibly help me with how to get this to happen?

Thank you in advance!

class MyListTableViewCell: UITableViewCell
{
    @IBOutlet weak var itemPrice: UILabel!
    @IBOutlet weak var itemName: UILabel!
    @IBOutlet weak var itemQuantity: UITextField!
    @IBOutlet weak var stepper: UIStepper!

    @IBAction func itemQuantityStepper(sender: UIStepper)
    {
        self.itemQuantity.text = Int(sender.value).description
    }

    override func awakeFromNib() {
        super.awakeFromNib()
    }

    func makeMyList(myItem: MyList)
    {
        self.itemName.text = myItem.MyItemName
        self.itemPrice.text = String(stepper.value * Double(myItem.MyItemPrice))
    }

    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
    }
}

回答1:

For anyone who has the same issue. Here is the answer to my own question.

Hope it helps!

class MyListTableViewCell: UITableViewCell
{
    @IBOutlet weak var itemPrice: UILabel!
    @IBOutlet weak var itemName: UILabel!
    @IBOutlet weak var itemQuantity: UITextField!
    @IBOutlet weak var stepper: UIStepper!

    var price: Float = 0.0
    var quantity: Int = 1


    override func awakeFromNib() {
        super.awakeFromNib()
    }

    func makeMyList(myItem: MyList)
    {
        self.itemName.text = myItem.MyItemName
        self.itemPrice.text = myItem.MyItemPrice

        price = Float(myItem.MyItemPrice)
    }

    @IBAction func itemQuantityStepper(sender: UIStepper)
    {
        quantity = Int(sender.value)
        self.itemQuantity.text = String(quantity)
        self.itemPrice.text = price * Float(quantity)
    }

    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
    }
}