Ad

Firestore - UISlider Inside UITableViewCell

I have a UISlider inside custom cells of a tableView. In the cell there is also a UILabel displaying the sliders value. At the moment I have got it so that I can update the sliders value to firestore and also into the label. However when I go off the screen/app and load the screen again, I can't load the previous sliders value into the slider. I still can update the sliders value and it will still transfer to firestore and everything works well. I have tried loading the value from firestore into the slider when the screen is first launched, which works perfectly, but It doesn't let me update the slider onscreen when I do this, but it does update on firestore. What would be perfect is so that the data is loaded from firestore when the screen is first loaded, I can then update the slider and it will work on the app and in firestore. Any help would be greatly appreciated! Many thanks!!

ViewController

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TableViewCell

    cell.slider.tag = indexPath.row
    cell.slider.addTarget(self, action: #selector(ViewController.sliderChange(sender:)), for: .valueChanged)

    cell.sliderLabel.text = String(Int(cell.slider.value))

    // code that loads values when screen is first launched but then doesn't let me update the onscreen values
    cell.slider.value = Float(array[indexPath.row].number)

    return cell
}

 @objc func sliderChange(sender: UISlider) {
    let currentValue = Int(sender.value)
    let row = sender.tag
    let Id = array[row].documentID

    let userRef = db.collection("Users").document(user!)
    userRef.collection("Friends").document(Id).updateData(["number" : currentValue]) {
        error in
        if let error = error {
            print("Error adding document: \(error.localizedDescription)")
        } else {
            print(currentValue)
        }
    }

    self.tableView.reloadData()

}

TableViewCell

class TableViewCell: UITableViewCell {

@IBOutlet weak var sliderLabel: UILabel!
@IBOutlet weak var slider: UISlider!

override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code

}

override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    // Configure the view for the selected state
}
Ad

Answer

Refactor the method to this , Note that update code is asynchronous you have to reflect the changed data to the table data source array and reload the table inside it's success part

 @objc func sliderChange(sender: UISlider) {

    let currentValue = Int(sender.value)
    let row = sender.tag
    let Id = array[row].documentID

    let userRef = db.collection("Users").document(user!)
    userRef.collection("Friends").document(Id).updateData(["number" : currentValue]) {
        error in
        if let error = error {
            print("Error adding document: \(error.localizedDescription)")
        } else {
            print(currentValue)
            array[indexPath.row].number = sender.value // set it according to type of number property 
            self.tableView.reloadData()  
        }

    }

}
Ad
source: stackoverflow.com
Ad