Kostas
Kostas

Reputation: 377

Swift: Stepper within a prototype cell, how to refresh the label text?

In my app I want to add a Stepper so the user can increase a value from 1 to 100.

I can get the stepper to show, I've added the action but the label in the tableview does not refresh... do I need to do a "self.tableView.reloadRowsAtIndexPaths" every time the user taps the stepper?

class SettingsOptionTableViewCell: UITableViewCell {

    @IBOutlet weak var labelvalueforstepper: UILabel!
    @IBOutlet weak var simpleStepper: UIStepper!
    @IBOutlet weak var labelwithStepper: UILabel!

}

class SettingOptionsTableViewController: UITableViewController {

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

            cell = tableView.dequeueReusableCellWithIdentifier("SettingOptionsCell3") as! SettingsOptionTableViewCell
            cell.labelwithStepper.text = "Value:"
            cell.simpleStepper.value = 1
            cell.labelvalueforstepper.text = (cell.simpleStepper.value).description
            cell.simpleStepper.wraps = false
            cell.simpleStepper.autorepeat = true
            cell.simpleStepper.maximumValue = 100

            cell.simpleStepper.addTarget(self, action: "stepperValueChanged:", forControlEvents: UIControlEvents.ValueChanged)

    }


    func stepperValueChanged(sender: UIStepper) {

        let pointInTable: CGPoint = sender.convertPoint(sender.bounds.origin, toView: self.tableView)
        let cellIndexPath = self.tableView.indexPathForRowAtPoint(pointInTable)

        if let myRowSection = cellIndexPath?.section {

            if let myRow = cellIndexPath?.row {

                var device_status: Int = Int(sender.value)

                println(sender.value)
                println(myRow)

                segmentDeviceViewValueChange(myRow, userSelection: device_status)

                let cell: SettingsOptionTableViewCell = self.tableView.dequeueReusableCellWithIdentifier("SettingOptionsCell3", forIndexPath: cellIndexPath!) as! SettingsOptionTableViewCell
                cell.labelvalueforstepper.text = device_status.description

            }

        }
    }
}

Upvotes: 1

Views: 1162

Answers (1)

theMikeSwan
theMikeSwan

Reputation: 4729

Instead of having the table view controller reach into each cell and do lots of fiddling let the cells take care of keeping the stepper and label in sync.

Add a property to the cell class that is the value of the stepper. Use a custom setter for he property that updates both the stepper and the label, and a custom getter that returns the current stepper value. The UIAction method for when the stepper is tapped then just has to worry about setting the label to the correct value.

If you need the table view controller to know as soon as a stepper value changes use a delegate protocol or post a notification that the value has changed.

Once you have all the code in place make sure that all the outlets are connected in the cell and that there is an action sent to the cell when the stepper is tapped.

Also, since you only ever set the text of labelwithStepper to "Value:" you can just edit the label right in IB and have one less outlet to connect.

Upvotes: 1

Related Questions