UISlider does not update current value in Swift

I have a slider inside the table view cell, and I'm trying to set the label text to the value of the slider. However, the text is not set for the changed values ​​of the slider. 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) } 
+5
source share
4 answers

You can do this because you have some tricks, first of all you need to set the goal for UISlider as some, as suggested, the other point is to get the selected line, which you can save in the tag property indexPath.row , as 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 } 

Another problem is the UILabel text, which you can get by getting the exact representation inside the cell, for example, 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 exact number inside the UITableViewCell to show the path, but the highly recommended way is to UIView over all the UIView and find your UILabel , as in the method suggested in this question Find the UILabel in the UIView in Swift , because the number may change.

As some people have said, it’s easier to set up a custom cell through Interface Builder, but it is up to you.

Hope this helps you.

+4
source

Generally, you should add an action to your slider:

 slider.addTarget(self, action: "sliderValueDidChange:", forControlEvents: .ValueChanged) func sliderValueDidChange(sender:UISlider!) { println("value--\(sender.value)") } 

as described here: http://www.apptuitions.com/programmatically-creating-uiview-uislider-uiswitch-using-swift/

** Since you need to change the label and use the value of the slider in any cell separately, you need to use your own TableViewCell class and easily define your slider, shortcut and action.

** MY RECOMMENDATION: use storyBoard and then easily design your presentation and use your own TableViewCell class.

cell class for representing a table of tables (similar to one in my own project):

 class CustomCell: UITableViewCell { @IBOutlet weak var slider: UISlider! @IBOutlet weak var label: UILabel! @IBAction func sliderChanged(sender: AnyObject) { // set label text to slider.value here } } 
+2
source

You are missing an action while dragging the slider. slider.addTarget (self, action: "sliderValueDidChange", forControlEvents: .ValueChanged)

 func sliderValueDidChange(sender:UISlider!){ println("value--\(sender.value)") label.text = String(sender.value) } 

You can use slider.superview or save the index in the slider tag to get a link to the cell to which the slider belongs and to access the label.

A better solution would be to create a custom class for the cell.

+1
source

Add this to your cellForRowAtIndexPath function:

 slider.addTarget(self, action: "sliderValueChanged:", forControlEvents: .ValueChanged) label.tag = 1 

You can use this to update the label:

 func sliderValueChanged(sender: AnyObject) { var position: CGPoint = sender.convertPoint(CGPointZero, toView: self.tableView) if let indexPath = self.tableView.indexPathForRowAtPoint(position) { //get your cell here let cell = tableView.cellForRowAtIndexPath(indexPath) //update label for view in cell.subviews { if let label = view as? UILabel { if label.tag == 1 { label.text! = "\((sender as! UISlider).value)" } } } } } 

OR

You can do what others have mentioned and create a custom UITableViewCell , add a slider and a label, and create IBOutlets for them.

+1
source

All Articles