Increment Label Value with UIStepper in UITableViewCell

我与影子孤独终老i 提交于 2021-01-28 03:06:46

问题


I want to be able to use a UIStepper that is located in each tableview row. I would like each UIStepper to update a label in the same tableview row that the UIStepper is in.

The code that I am trying is as follows

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cartListCell = tableView.dequeueReusableCell(withIdentifier: reuseCartListingCellIdentifier, for: indexPath) as? CartListingItemTableViewCell

        cartListCell?.UI_STEPPER_NAME.value = VALUE_FROM_ARRAY

        return cartListCell!

}


@IBAction func CartStoreQtyStepperAction(_ sender: UIStepper) {


        // I need to update the value of a label in the tableview cell that tapped

    }

UPDATED IMPLEMENTATION

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        cartListCell?.CartListingProductQtyStepperOutlet.tag = indexPath.row
     cartListCell?.CartListingProductQtyStepperOutlet.addTarget(self, action: #selector(self.CartStoreQtyStepperAction(_:)), for: UIControlEvents.valueChanged)
        cartListCell?.tag         =   Int(CartStoreItemIdArray [indexPath.row])!
        return cartListCell!
}


@IBAction func CartStoreQtyStepperAction(_ sender: UIStepper) 
{

            let stepperValue = Int(sender.value)
            let indexPath = IndexPath(row: stepperValue, section: 0)
            print(stepperValue)

           if let cell = yourTableView.cellForRow(at: indexPath) as? CartListingItemTableViewCell 
    {
        print(cell?.tag)
    }

}

I am not able to access the tableview cell and the label in that when I am doing it like this. Can someone guide me how to do this?


回答1:


// Custom tableview cell code
class CartListingItemTableViewCell: UITableViewCell {

    @IBOutlet weak var label: UILabel!
    @IBOutlet weak var stepper: UIStepper!

}

// your view controller code (tableview data source)

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cartListCell = tableView.dequeueReusableCell(withIdentifier: reuseCartListingCellIdentifier, for: indexPath) as? CartListingItemTableViewCell

    let stepperValue = yourArray[indexPath.row]
    cartListCell?.label.text = String(stepperValue) ?? "0"
    cartListCell?.stepper.value = Int(stepperValue) ?? 0
    cartListCell?.stepper.tag = indexPath.row
    cartListCell?.stepper.addTarget(self, action: #selector(self.stepperValueChanged(_:)), for: UIControlEvents.valueChanged)

    return cartListCell!

}


// handle stepper value change action
@IBAction func stepperValueChanged(_ stepper: UIStepper) {

    let stepperValue = Int(stepper.value)
    print(stepperValue) // prints value

    let indexPath = IndexPath(row: stepperValue, section: 0)
    if let cell = yourTableView.cellForRow(at: indexPath) as? CartListingItemTableViewCell {
        cell.label.text = String(stepperValue)
        yourValueArray[index] = stepperValue

    }
}



回答2:


This is a solution for Swift 4 using NSKeyValueObservation

  • First of all you need a property in your data model to keep the current value of the stepper.

    var counter = 0.0
    
  • In the custom cell create outlets for the stepper and the label and a property for the observation. Connect the outlets.

    @IBOutlet weak var stepper : UIStepper!
    @IBOutlet weak var label : UILabel!
    
    var observation : NSKeyValueObservation?
    
  • In the controller in cellForRow add the observer, dataSourceArray is the data source array.

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
        let cartListCell = tableView.dequeueReusableCell(withIdentifier: reuseCartListingCellIdentifier, for: indexPath) as! CartListingItemTableViewCell
        let item = dataSourceArray[indexPath.row]
        cartListCell.stepper.value = item.counter
        cartListCell.label.text = "\(item.counter)"
    
        cartListCell.observation = cell.stepper.observe(\.value, options: [.new]) { (_, change) in
            cartListCell.label.text = "\(change.newValue!)"
            item.counter = change.newValue!
        }
    
        return cartListCell
    }
    



回答3:


Solution for Swift 4 swift4.

  1. Add these lines in cellforRowAt.

    cartListCell.UI_STEPPER_NAME.tag = indexPath.row
    cartListCell.UI_STEPPER_NAME.addTarget(self, action: #selector(CartStoreQtyStepperAction), for: .touchUpInside)
    
  2. To access cell items in the stepper function use this line.

    let cartListCell = sender.superview?.superview as! CartListingItemTableViewCell
    
    //check whether your superview is table cell.
    //then use the cell instance to update the cell label
    
  3. Use sender.tag in the function to access particular cell for example.

    cartListCell.UI_STEPPER_NAME.text = "\(VALUE_FROM_ARRAY[sender.tag])"
    
    //sender.tag is similar to indexPath.row as we pass the tag value in the cellforRowAt. 
    //It will work then please accept my answer👍🏼😄.       
    



回答4:


Assign tag value to sender of UIStepper in cellForRowAt and access the change of UIStepper value in CartStoreQtyStepperAction action using same tag.

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cartListCell = tableView.dequeueReusableCell(withIdentifier: reuseCartListingCellIdentifier, for: indexPath) as? CartListingItemTableViewCell

        //Assign tag value
        cartListCell?.UI_STEPPER_NAME.tag = indexPath.row
        cartListCell?.UI_STEPPER_NAME.value = VALUE_FROM_ARRAY
        cartListCell?.stepper.addTarget(self, action: #selector(self.CartStoreQtyStepperAction(_:)), for: UIControlEvents.valueChanged)

        return cartListCell!
}


@IBAction func CartStoreQtyStepperAction(_ sender: UIStepper) {

    let index = sender.tag
    yourValueArray[index] = sender.value
    //Reload particular cell for tableView
}


来源:https://stackoverflow.com/questions/49487730/increment-label-value-with-uistepper-in-uitableviewcell

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