Search code examples
iosswiftuilabeluislider

UISlider not updating current value in Swift


I have a slider inside a table view cell and I'm trying to set the text of a label to the value of the slider. However, the text isn't setting to the changed slider values. What am I doing wrong?

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

    if indexPath.section == 0 {

        let slider = UISlider(frame: CGRectMake(0, 0, 320, 44))
        slider.userInteractionEnabled = true
        slider.minimumValue = 1
        slider.maximumValue = 100
        slider.value = 50
        slider.continuous = true
        cell.addSubview(slider)
        println(cell.bounds)

        let label = UILabel(frame: CGRectMake(260, -20, 100, 20))
        var theValue = Int(slider.value)
        label.text = "\(theValue)" + "mi."
        cell.addSubview(label)
 }

Solution

  • You can do it as you have using some tricks, first of all you need to set the target for the UISlider as some as suggested, the other point is get the selected row, which you can achieve saving in the tag property the indexPath.row like in the following code:

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    
        let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell
    
        let slider = UISlider(frame: CGRectMake(0, 0, 100, 44))
        slider.userInteractionEnabled = true
        slider.minimumValue = 1
        slider.maximumValue = 100
        slider.value = 50
        slider.continuous = true
    
        // set the target to respond to changes.
        slider.addTarget(self, action: "sliderValueChanged:", forControlEvents: UIControlEvents.ValueChanged)
    
        // save the indexPath.row
        slider.tag = indexPath.row
        cell.addSubview(slider)
    
    
        let label = UILabel(frame: CGRectMake(105, 0, 100, 20))
        var theValue = Int(slider.value)
        label.text = "\(theValue)" + "mi."
        cell.addSubview(label)
    
        return cell
    }
    

    The another problem is set the UILabel text , which you can achieve by getting the exactly view inside the cell, like in the following code:

    func sliderValueChanged(sender: AnyObject) {
    
        var slider = sender as! UISlider
        let cell = self.tableView.cellForRowAtIndexPath(NSIndexPath(forRow: slider.tag, inSection: 0)) as UITableViewCell!
    
        // If you know the exactly index
        // var label = cell.subviews[4] as! UILabel
        // label.text = "\(slider.value)"
    
        // assuming you have only one `UILabel` inside your cell
        for view in cell.subviews {
            if let label = view as? UILabel {
                label.text = "\(slider.value)"
            }
        }
    }
    

    In the above code I only set the exactly number inside the UITableViewCell for purpose of show the way, but the strongly recommended way is iterate through all the UIView and find your UILabel like in the method proposed in this question Find UILabel in UIView in Swift because the number can change.

    As some people said before, is more easy set a Custom Cell all through Interface Builder, but it's up to you.

    I hope this help you.