0

I have added UIStepper in Custom UItableviewcell Programatically.

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCellWithIdentifier("subServiceCell", forIndexPath: indexPath) as? RE_RepairSubServiceCell
    let stepperUnit:UIStepper = UIStepper()
    stepperUnit.frame = CGRectMake(200,75, 94, 29);
    stepperUnit.tag = indexPath.row + 1000
    stepperUnit.value = 0.0
    stepperUnit.wraps = true
    stepperUnit.autorepeat = false
    stepperUnit.stepValue = 1.0
    stepperUnit.minimumValue = 0.0
    stepperUnit.maximumValue = 100.0
    stepperUnit.continuous = true
    stepperUnit.addTarget(self, action:#selector(RE_RepairSubServiceVC.stepperChanged(_:)), forControlEvents: .ValueChanged)
    cell!.addSubview(stepperUnit)
    return cell!
}

Add action for stepper

func stepperChanged(stepper:UIStepper) {
    let row:Int = (stepper.superview?.tag)!
    let value:Int = Int(stepper.value)
    print("Row : \(row) Value: \(value)")

} 

When i pressed a stepper in cell which indexpath.row = 0. it give me same output such as

Row : 0 Value: 1
Row : 0 Value: 1
Row : 0 Value: 1
Row : 0 Value: 1
Row : 0 Value: 1

What is the mistake in my code? i am not able to figure it out this code which is giving me above result.

Please anyone can help me to solve the problem.

Thanks in advance.

  • your code is working proper when I checked it with the default cell, there may be some issue with the custom cell creation , your stepper value will reset if your cell goes outside the visible area – HardikDG Apr 10 '16 at 06:38
  • Thank you for your precious time @Pyro . I have solved the issue. The problem was cell was recreated when i press the stepper. That's why the value of stepper was reset. – Priyank Gandhi Apr 10 '16 at 07:03

0 Answers0