1

The table view and cells have been created and populated with a label and slider.I have an IBAction on the slider when the value is changed.

How do I get that value back into the label in the cell?

I have an IBOutlet linked in the table view.

@IBAction func slideChange(_ sender: UISlider) {

    let currentValue = sender.value    // gets slider's value
    let row = sender.tag               // gets slider's row in table

    myTableView.cell.myLabel.text = sender.value  // Here i need to show updated value


    print("Slider in row \(row) has a value of \(currentValue)")  // works
}
Phani Sai
  • 1,215
  • 19
  • 33
Sean
  • 629
  • 8
  • 24
  • you need to reload either tableview or specific cell and than in the cellforRowAtIndexpath method, try to set that value to that cell. – Shabir jan May 24 '17 at 10:09
  • 1
    Handle the slider in your cell and use a delegate or a closure see https://stackoverflow.com/questions/28659845/swift-how-to-get-the-indexpath-row-when-a-button-in-a-cell-is-tapped/38941510#38941510 – Paulw11 May 24 '17 at 10:20

2 Answers2

-1

Try this:

@IBAction func slideChange(_ sender: UISlider){
     let cell: UITableViewCell? = (sender.superview?.superview as? UITableViewCell)  // track cell using your view hierarchy and this your cell
     cell.myLabel.text = sender.value
}

OR

  let rootViewPoint: CGPoint? = sender.superview?.convert(sender.center, to: tblView)
   var indexPath: IndexPath? = tblView?.indexPathForRow(at: rootViewPoint!)
   let cell: UITableViewCell? = tblView?.cellForRow(at: indexPath!)
KKRocks
  • 8,222
  • 1
  • 18
  • 84
  • @Sean did you try this answer ? – KKRocks May 24 '17 at 10:28
  • Yeah. I was wrong about the superview count. I needed to change the UITableViewCell type to PersonTableCell (as I have my class) and remove the optional on it. And I needed to force unwrap the whole cell. Works – Sean May 24 '17 at 10:46
  • Sometimes answer is for only suggestion . you have to make some changes on answer for get desired output. – KKRocks May 24 '17 at 10:47
  • then why down voting .. you can ask alternate for view count . – KKRocks May 24 '17 at 10:49
  • I didn't downvote. I can't even vote with my reputation score yet. :( I upvoted! – Sean May 24 '17 at 10:51
  • Then why did you accept answer. @KKRocks already answered that? why did you say brilliant in that answer? Reviewer team pls have a look. – elk_cloner May 24 '17 at 11:35
  • @Sean what is the difference in my answer and accepted answer ? – KKRocks May 24 '17 at 11:36
  • why down voting ? – KKRocks May 24 '17 at 12:47
  • 1) I didn't down vote any answer. 2) The accepted was the one I ended up using. Both work. I personally find the accepted one slightly more elegant than traversing the object tree. – Sean May 25 '17 at 04:48
-2

Get a reference to the table view cell

let cell = tableView.cellForRow(at: IndexPath(row: sender.tag, section: 0))

Update its label

cell?.myLabel.text = sender.value

Note: This method returns nil when cell is not visible. In your case this won't be an issue.

Jože Ws
  • 1,754
  • 17
  • 12
  • Brilliant. And so simple when you know how. Thanks for the fast response. – Sean May 24 '17 at 10:26
  • I think what you are missing is to update your data model, so that _cellForItem_ method configures the cell with the updated value when scrolling. – Jože Ws May 24 '17 at 11:32