如何仅允许用户打开以编程方式构建的单个uiSwitch并存储开关的数据

时间:2018-10-22 23:24:17

标签: swift uitableview core-data uiswitch

我在car register tableview中有一个以编程方式构建的uiswitch,当用户为自己选择的汽车打开它时,布尔值将保存并存储在核心数据中,并且汽车品牌将以不同的视图显示控制器。

问题: 如何使用户只能打开一个开关,以及如何在打开开关时保存该数据(汽车名称)?

代码:

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")

这是我尝试的代码:

    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

1 个答案:

答案 0 :(得分:0)

我以前执行过这样的任务。 这个想法是保持引用当前选定的索引, 当用户切换汽车时,用它来转动选定的开关off

以下代码未经测试。请使用它们作为参考

// 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()  
 }