UISlider not updating current value in Swift
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.
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/
** As you need to change label and use slider's value in any cell, separately, You have to use custom TableViewCell class and define your slider, label and action in it, easily.
** MY RECOMMENDATION: Use storyBoard and then easily design your view and use customized TableViewCell class.
a sample table view cell class for you ( 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
}
}