-1

unfortunately I can not use tag value to get switch Changed row detected ... Mainly I need to know particular switch changed row text "bad morning" to search something from my array . so anyone help me please how can I get cell textLabel text in the switchChanged method . Thanks

Code sample :

func switchChanged(_ sender : UISwitch!){

      print("table row switch Changed \(sender.tag)")
      print("The switch is \(sender.isOn ? "ON" : "OFF")")
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {


                var cell = tableView.dequeueReusableCell(withIdentifier: "yourcellIdentifire", for: indexPath) as! YourCellClass

                        cell.textLabel?.text = "bad morning"

                        let switchView = UISwitch(frame: .zero)
                        switchView.setOn(false, animated: true)

                        switchView.tag = indexPath.row // for detect which row switch Changed

                        switchView.addTarget(self, action: #selector(self.switchChanged(_:)), for: .valueChanged)

                        cell.accessoryView = switchView

               return cell

      }
Nazmul Hasan
  • 10,130
  • 7
  • 50
  • 73
  • Isn't the switch ALWAYS in row 0. Do you have multiple sections, each with a switch in row 0? If not, the cell is always at IndexPath 0,0 – dmorrow Mar 22 '17 at 16:36
  • @dmorrow switch will be load based on data may 1 or more – Nazmul Hasan Mar 22 '17 at 16:39
  • See my delegate suggestion - that's the best way to handle it. – dmorrow Mar 22 '17 at 16:40
  • Take a look at this question: http://stackoverflow.com/questions/42876739/swift-increment-label-with-stepper-in-tableview-cell/42877313#42877313. The way in my answer can be also used in your case. – vadian Mar 22 '17 at 17:13
  • @vadian thanks i solved my problem OOP concept with Tag but it is one more way i can do it to solve my problem – Nazmul Hasan Mar 22 '17 at 17:17

2 Answers2

1

You can add a delegate to your YourCellClass. When the switchView is toggled, call a delegate method something like this

class YourCellClass: UITableViewCell {
    weak var delegate:YourCellClassDelegate?
    var switchView: UISwitchView

    init() {
        ...other stuff...
        switchView.addTarget(self, action: #selector(self.switchChanged(_:)), for: .valueChanged)
        ...other stuff...
    }

    func switchChanged(_ sender : UISwitch!){ 
        if let delegate = delegate {
            delegate.cellDidToggleSwitch(self, isOn: sender.isOn)
        }
    }
}

protocol YourCellClassDelegate {
    func cellDidToggleSwitch(_ cell: YourCellClass, isOn: Bool)
}

Then in the ViewController with the TableView

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if indexPath.row == 0 {  // specific row showing switch          
        var cell = tableView.dequeueReusableCell(withIdentifier: "yourcellIdentifire", for: indexPath) as! YourCellClass
        cell.textLabel?.text = "bad morning"
        cell.delegate = self
        return cell
    }
    return UITableViewCell()
}

func cellDidToggleSwitch(_ cell: YourCellClass, isOn: Bool) {
    //here's your cell
}
dmorrow
  • 5,152
  • 5
  • 20
  • 31
0

Have you tried to pass to the function switchChanged the IndexPath too? I mean two parameters, the sender and the indexPath.

Aitor Pagán
  • 423
  • 7
  • 18