I am struggling to properly use UISliders in a UITableViewCell. Here is the Idea:
Start simpler...
First, let's add a "current value" property to your SliderClass
(I'm calling it a SoloJob
class, as it seems more logical):
class SoloJob: NSObject {
var title: String = ""
var subtitle: String = ""
var sliderMinimum: Float = 0
var sliderMaximum: Float = 100
var currentValue: Float = 0
init(title: String, subtitle: String, sliderMinimum: Float, sliderMaximum: Float, currentValue: Float) {
self.title = title
self.subtitle = subtitle
self.sliderMinimum = sliderMinimum
self.sliderMaximum = sliderMaximum
self.currentValue = currentValue
}
}
We'll use the currentValue
property to keep track of the slider value.
So, create a cell with a "title" label, a slider, and a "job amount" (or current value) label. I have it laid out like this:
In your cell class, connect the slider to an @IBAction
for when it changes - NOT in your controller class.
Also in your cell class, add a "callback" closure var:
// closure to tell controller the slider was changed
var callback: ((Float) -> ())?
then, in your @IBAction
func:
@IBAction func sliderValueChange(_ sender: UISlider) -> Void {
let v = sender.value
// update the label
jobAmountLabel.text = "Current Amount: \(Int(v))"
// tell the controller the slider changed
callback?(v)
}
Back in your controller class, in cellForRowAt
, setup the "callback" closure:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ProjectCharacterTableViewCell", for: indexPath) as! ProjectCharacterTableViewCell
let thisJob: SoloJob = sortedSoloJobs[indexPath.row]
// set values / labels in the cell
// closure to get notified when the slider is changed
cell.callback = { val in
// update our data
self.sortedSoloJobs[indexPath.row].currentValue = val
}
return cell
}
When the user drags the slider, @IBAction func sliderValueChange()
in the cell class itself will be called, and that's where we update the label in the cell and tell the controller the value changed.
Here is a complete implementation:
import UIKit
class SoloJob: NSObject {
var title: String = ""
var subtitle: String = ""
var sliderMinimum: Float = 0
var sliderMaximum: Float = 100
var currentValue: Float = 0
init(title: String, subtitle: String, sliderMinimum: Float, sliderMaximum: Float, currentValue: Float) {
self.title = title
self.subtitle = subtitle
self.sliderMinimum = sliderMinimum
self.sliderMaximum = sliderMaximum
self.currentValue = currentValue
}
}
class ProjectCharacterTableViewCell: UITableViewCell {
@IBOutlet weak var jobAmountLabel: UILabel!
@IBOutlet weak var slider: UISlider!
@IBOutlet weak var jobNameLabel: UILabel!
// closure to tell controller the slider was changed
var callback: ((Float) -> ())?
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
commonInit()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
commonInit()
}
func commonInit() -> Void {
contentView.layer.cornerRadius = 12
contentView.layer.borderColor = UIColor.blue.cgColor
contentView.layer.borderWidth = 1
contentView.layer.masksToBounds = true
contentView.backgroundColor = .white
backgroundColor = .clear
}
func configureCell(_ theJob: SoloJob) -> Void {
jobNameLabel.text = theJob.title + " - min: \(Int(theJob.sliderMinimum)) / max: \(Int(theJob.sliderMaximum))"
slider.minimumValue = theJob.sliderMinimum
slider.maximumValue = theJob.sliderMaximum
slider.value = theJob.currentValue
jobAmountLabel.text = "Current Amount: \(Int(theJob.currentValue))"
}
// connect valueChanged action in Storyboard
@IBAction func sliderValueChange(_ sender: UISlider) -> Void {
let v = sender.value
// update the label
jobAmountLabel.text = "Current Amount: \(Int(v))"
// tell the controller the slider changed
callback?(v)
}
}
class ProjectTeamViewController: UIViewController {
@IBOutlet weak var tableView: UITableView!
var sortedSoloJobs: [SoloJob] = []
override func viewDidLoad() {
super.viewDidLoad()
// create some example data
for i in 1...20 {
// random slider minimum between 0 and 2
let minVal = Int.random(in: 0...2)
// random slider maximum between 5 and 10
let maxVal = Int.random(in: 5...10)
// start with current value at minimum
let curVal = minVal
let job = SoloJob(title: "Job Name \(i)", subtitle: "", sliderMinimum: Float(minVal), sliderMaximum: Float(maxVal), currentValue: Float(curVal))
sortedSoloJobs.append(job)
}
tableView.dataSource = self
tableView.delegate = self
}
}
extension ProjectTeamViewController: UITableViewDataSource {
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return sortedSoloJobs.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ProjectCharacterTableViewCell", for: indexPath) as! ProjectCharacterTableViewCell
let thisJob: SoloJob = sortedSoloJobs[indexPath.row]
cell.configureCell(thisJob)
// closure to get notified when the slider is changed
cell.callback = { val in
// update our data
self.sortedSoloJobs[indexPath.row].currentValue = val
}
return cell
}
}
extension ProjectTeamViewController: UITableViewDelegate {
}
and the Storyboard source (with all the @IBOutlet
and @IBAction
connections):
Result:
And because we're updating our data array whenever a slider is changed, we can scroll through the table and reused cells will be configured properly.
When all of that makes sense, carry the methods over to your project to match your layout and data structuring.