0

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)
    }
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
yumyum
  • 1
  • 1
  • Simply update `itemPrice.text` in the `itemQuantityStepper` function. – rmaddy Nov 12 '15 at 03:59
  • @rmaddy I have tried that. itemQuantityStepper cannot take (myItem: MyList) as a parameter. So in doing that the value only increments. if attempting to decrement, it stays at the previously incremented value. – yumyum Nov 12 '15 at 04:17
  • Save a reference to the `MyList` or its price as an instance variable. Then you can reference that in the `itemQuantityStepper` method. – rmaddy Nov 12 '15 at 04:19
  • @rmaddy Could you possibly elaborate on that a little bit. possibly point me to, or show me an small example code snippet? – yumyum Nov 12 '15 at 04:46
  • @rmaddy after reading what you said a million times over, I finally realized what you meant and fixed my problem. Thanks for the help! – yumyum Nov 13 '15 at 03:32

1 Answers1

0

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)
    }
}
yumyum
  • 1
  • 1