Swift Stepper Action that changes UITextField and UILabel within same cell

左心房为你撑大大i 提交于 2019-12-11 20:17:27

问题


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)
    }
}


来源:https://stackoverflow.com/questions/33664189/swift-stepper-action-that-changes-uitextfield-and-uilabel-within-same-cell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!