2

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?

Jobins John
  • 1,265
  • 23
  • 45
  • try https://stackoverflow.com/questions/48739894/how-to-get-indexpath-when-image-inside-cell-tapped/48740036#48740036 and `solution2` – Pratik Prajapati Mar 26 '18 at 09:22

4 Answers4

4
// 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

    }
}
Krunal
  • 77,632
  • 48
  • 245
  • 261
  • Althought i am able to get the stepper value on the function, i am not able to assign the incremented function to the label in the cell – Jobins John Mar 26 '18 at 11:15
  • Check the question. I have added the implementation that i am trying to do – Jobins John Mar 26 '18 at 11:38
  • check question. I have modified it – Jobins John Mar 26 '18 at 11:46
  • @JobinsJohn - Something is wrong with code you have implemented and sharing here.. `print(cell?.tag) // prints nil` this statement never executes inside `if-let`. As `if-let` ensures your cell value is not nil and if cell value is nil then it will not enter in `if` block – Krunal Mar 26 '18 at 11:51
  • Note that IndexPath(row: stepperValue, section: 0) should most likely be IndexPath(row: stepper.tag, section: 0) – tim.baker May 10 '20 at 10:58
0

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
}
Jaydeep Vora
  • 6,085
  • 1
  • 22
  • 40
0

Solution for Swift 4 .

  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.       
    
Mihayl
  • 3,821
  • 2
  • 13
  • 32
0

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
    }
    
vadian
  • 274,689
  • 30
  • 353
  • 361