title: Swift Using a UISlider in a UITableViewCell the right way?
body: <p>I am struggling to properly use UISliders in a UITableViewCell.
  Here is the Idea:</p>
  <ul>
  <li>The User can set different Jobs which he needs to fulfil tasks. The amount of different Jobs cannot be known in advance. Lets say for example User1 needs for his Band only Guitarists and Drummers, where someone else needs Guitarists, Drummers, Singers, Keyboarders and so on.</li>
  </ul>
  <p>What I want him to provide is that depending on which Jobs he needs, he can set the amount of people he needs for a specific job.
  I am using a UISlider in a TableViewCell to let him do this.</p>
  <p>But here comes the trouble:</p>
  <p>(1) The Label under the UISlider doesn't show the right amount while the User drags the Slider
  (2) The prepareForReuse function doesn't work properly. I want the User to have the Slider back at 0 and the Label showing 0 when a reused Cell gets used.
  (3) Finally the <code>jobAmountNeeded </code>Array doesn't have the right values stored (or no values at all) and therefore the saved Amounts in Firebase aren't right.</p>
  <p>In my opinion all three Problems are connect to the same source.</p>
  <p>Therefore I would be more than pleased, if someone could help me. :)</p>
  <p>Thanks for your help everyone!</p>
  <pre><code>class ProjectTeamViewController: UIViewController {
  
      // MARK: - Properties
      
      @IBOutlet weak var tableView: UITableView!
      @IBOutlet weak var headerSeperator: UIView!
      @IBOutlet weak var saveButton: UIButton!
      
      
      var sortedSoloJobs = [String]()
      
      var jobIndex: Int?
      var jobAmount: [String] = []
      var selectedJob:String?
      var sentProjectCreated:String?
      var projectCreated:String?
      var jobAmountNeeded = [JobNeeded]()
      
  
         
      var slider: [SliderClass] = []
      
      let step:Float = 1
      
      var selectedIndexPath: IndexPath?
      
      var tempArray = [99: &quot;Baum&quot;]
      var tempArray2 = [99: 5]
      var currentValue:Float = 0
      var key = 0
      
  
  
      override func viewDidLoad() {
          super.viewDidLoad()
  
          setUpUI ()
          getJobs(for: User.current) { (memberJobs) in
              self.tableView.reloadData()
          }
  
      }
      
      
      // MARK: - Functions
      
          func setUpUI () {
              Utilities.addShadowtoView(headerSeperator)
              Utilities.addShadowtoButton(saveButton)
              saveButton.layer.cornerRadius = 6
              saveButton.layer.borderWidth = 1
              saveButton.layer.borderColor = UIColor.darkGray.cgColor
              
          }
  
          func getJobs(for user: User, completion: @escaping ([MemberJobsStruct]) -&gt; Void) {
              var jobs = [String]() // All Jobs
              var soloJobs = [String]() // Solo jobs from all jobs
  
              let ref = Database.database().reference().child(&quot;team&quot;).child(user.uid)
  
              ref.observeSingleEvent(of: .value, with: { snapshot in
  
                  for case let child as DataSnapshot in snapshot.children {
                      guard let value = child.value as? [String: Any] else {
                          return completion ([]) }
  
                      let memberJob = value[&quot;memberJob&quot;] as! String
                      jobs.append(memberJob)
                      soloJobs = Array(Set(jobs))
                      self.sortedSoloJobs = soloJobs.sorted()
                      
                      DispatchQueue.main.async { self.tableView.reloadData() }
              } })
          }
      func json(from object:Any) -&gt; String? {
          guard let data = try? JSONSerialization.data(withJSONObject: object, options: []) else {
              return nil
          }
          return String(data: data, encoding: String.Encoding.utf8)
      }
    
     @objc func sliderValueChange(sender: UISlider){
           
      sender.isContinuous = false
      currentValue = round(sender.value / step) * step
      var sliderValue = Int(currentValue)
              sender.value = currentValue   // get slider's value
              let row = sender.tag               // get slider's row in table
                  key = row
      
      
     
      if let row = self.jobAmountNeeded.firstIndex(where: {$0.jobKey == key}) {
      jobAmountNeeded[row].jobAmount = sliderValue
          }
      }
      
      @IBAction func saveAction(_ sender: Any) {
  
          if projectCreated == nil || projectCreated == &quot;no Value&quot; {
              let alert = UIAlertController(title: &quot;Error&quot;, message: &quot;Internal Error send a bug report: #150420&quot;, preferredStyle: .alert)
                  alert.addAction(UIAlertAction(title: &quot;OK&quot;, style: .default, handler: nil))
                  alert.addAction(UIAlertAction(title: &quot;Cancel&quot;, style: .default, handler: nil))
                  self.present(alert, animated: true)
          } else {
          for value in jobAmountNeeded {
              let jobName = value.jobName
              let jobAmount = value.jobAmount
              let jobKey = value.jobKey
              let currentUser = User.current
              let jobreqRef = Database.database().reference().child(&quot;posts&quot;).child(currentUser.uid).child(projectCreated ?? &quot;no Value&quot;).child(&quot;ProjectJobAmount&quot;).child(value.jobName)
              let project = JobNeeded(jobName: jobName, jobAmount: jobAmount, jobKey: jobKey)
              let projectDict = project.dictValue
                     jobreqRef.setValue(projectDict)  { err, jobreqRef in
  //                       print(&quot;done&quot;)
              }
          }
      
          }
      }
      
      @IBAction func cancelAction(_ sender: Any) {
            self.view.window?.rootViewController?.dismiss(animated: true, completion: nil)
          
      }
      
  }
  
  extension ProjectTeamViewController: UITableViewDataSource {
  
      func numberOfSections(in tableView: UITableView) -&gt; Int {
  
          return 1
      }
  
      func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {
          
          return sortedSoloJobs.count    }
  
      
      func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&gt; UITableViewCell {
          
          let sortedSoloJob = sortedSoloJobs[indexPath.row]
          
          
          let key = indexPath.row
  
          let cell = tableView.dequeueReusableCell(withIdentifier: &quot;ProjectCharacterTableViewCell&quot;, for: indexPath) as! ProjectCharacterTableViewCell
    
          cell.jobNameLabel.text = sortedSoloJobs[indexPath.row]
                 
                 jobAmountNeeded.append(JobNeeded(jobName: sortedSoloJob, jobAmount: 0, jobKey: indexPath.section))
                 
                 if let row = self.jobAmountNeeded.firstIndex(where: {$0.jobName == sortedSoloJob}) {
                     jobAmountNeeded[row].jobName = sortedSoloJob
                     }
             
               
          tempArray.updateValue(sortedSoloJob, forKey: key)
  
          let currentValue = cell.slider.value
          cell.slider.isContinuous = false
          cell.slider.tag = key
          cell.slider.minimumValue = 0
          cell.slider.maximumValue = Float(sortedSoloJob.count)
          cell.slider.addTarget(self, action:  #selector(sliderValueChange), for: .valueChanged)
          cell.jobAmountLabel.text = &quot;\(currentValue)&quot;
          return cell
              
            
          
      }
   
  }
  // MARK: - UITableViewDelegate
  
  extension ProjectTeamViewController: UITableViewDelegate {
      
      func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -&gt; CGFloat {
  
          return  163
         
      }
  
  }
  
  </code></pre>
  <pre><code>
  class SliderClass: NSObject {
      var title: String?
      var subtitle: String?
      var sliderMinimum: Float?
      var sliderMaximum: Float?
  
      init(title: String, subtitle: String, sliderMinimum: Float, sliderMaximum: Float) {
          self.title = title
          self.subtitle = subtitle
          self.sliderMinimum = sliderMinimum
          self.sliderMaximum = sliderMaximum
  
      }
  }
  
  class ProjectCharacterTableViewCell: UITableViewCell {
  
      
      @IBOutlet weak var jobAmountLabel: UILabel!
      @IBOutlet weak var slider: UISlider!
      @IBOutlet weak var jobNameLabel: UILabel!
      
      
      
      
         
         override func awakeFromNib() {
             super.awakeFromNib()
              
          
      }
      
      override public func prepareForReuse() {
        // Ensures the reused cosmos view is as good as new
          super.prepareForReuse()
          self.jobAmountLabel.text = &quot;0&quot;
          self.slider.value = 0
      }
  
      override func setSelected(_ selected: Bool, animated: Bool) {
          super.setSelected(selected, animated: animated)
  
          // Configure the view for the selected state
      }
  
  }
  
  </code></pre>