Dadummn
Dadummn

Reputation: 31

How to allow user to turn on ONLY a single uiSwitch that is built programmatically and store data of the switch

I have a programmatically built uiswitch in my car register tableview that when the user switches it on for the car of their choice, the boolean is saved and stored in core data and the car brand will be displayed in a different view controller.

Question: How can I make it that the user can only turn on ONE switch and how can i save that data (Car name) when the switch is turned on?

Code:

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

    let cellS = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! TableViewCell

    //here is programatically switch make to the table view

    switchView.setOn(false, animated: true)
    switchView.tag = indexPath.row // for detect which row switch Changed
//        switchView.addTarget(self, action: #selector(self.switchChanged(_:)), for: .valueChanged)
    cellS.accessoryView = switchView

    if indexPath.row != 0 {
        switchView.isEnabled = true
        UserDefaults.standard.set(switchView.isOn, forKey: "CarSwitch")

This is my attempted code:

    if switchView.isSelected {
            UserDefaults.standard.set(indexPath.row, forKey: "usethiscar")
        }

    }

    if tableView == table1 {
        let cell = table1.dequeueReusableCell(withIdentifier: "Cell")

        let row = indexPath.row
        cell?.textLabel?.text = table1Data[row]

        return cell!
    }

    return UITableViewCell()
} 

This is how it looks like

This is when 'Add car is' pressed

This is not supposed to happen when two switches are on

Upvotes: 2

Views: 255

Answers (1)

Allan  Macatingrao
Allan Macatingrao

Reputation: 2101

I implemented a task like this before. The idea is to keep reference to the currently selected index and use it in turning the selected switch off when the user switches car.

The code below is not tested. Please use them as your reference.

// the table data model
var carNames: [String] = []

// this will hold the current selected tag index
var selectedTagIndex: Int = -1

// configuring the cell
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
{
    // copied from your code.
    // NOTE: You might wan't to create a custom cell for this
    // to access the switch view directly when turning it off
    // during the car selection
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! TableViewCell

    // switch on if the index path row is equal to the current
    // selected index, switch off otherwise
    cell.switchView.isOn = (indexPath.row == self.selectedTagIndex)

    // set the tag index
    cell.switchView.tag = indexPath.row
 }

 // Switch change-in-value handler
 func onCarSwitchValueChanged(sender: UISwitch)
 {
    guard sender.isOn else 
    {
       // if switch is off

       // remove selected tag index
       self.selectedTagIndex = -1

       // don't execute the code below
       return 
     }

     // the tag tag index of the switch
     let switchIndexTag =  sender.tag

     // check if there is a previously selected switch 
     guard self.selectedTagIndex != -1  else
     {
       // if none, just save the selected tag index
       self.selectedTagIndex = switchIndexTag

       // don't execute the code below
       return 
     }

     // OPTION 1: Turn off the selected switch manually.
     // Create a custom cell to be able to access the 
     // the switch view directly

     // create index path using the tag index
     let indexPath = IndexPath(row: switchIndexTag, section: 0)
     let cell = tableView.cellForRow(at: indexPath!) as! TheCustomCellClass
     cell.switchView.isOn = false 

     // save the current selected switch tag index
     self.selectedTagIndex = switchView.tag

     // OPTION 2: Reload the entire table 
     // self.tableView.reloadData()  
 }

Upvotes: 1

Related Questions