我在我的汽车寄存器 TableView
中有一个以编程方式构建的uiswitch,当用户为他们选择的汽车打开它时, bool 值将被保存并存储在核心数据中,并且汽车品牌将被保存显示在不同的 View Controller 中。
问题: 如何使用户只能打开一个开关以及如何在开关打开时保存该数据(汽车名称)?
代码:
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()
}
最佳答案
我之前实现过这样的任务。 这个想法是保留对当前选定索引的引用并且 当用户切换汽车时,使用它来关闭所选开关。
下面的代码未经测试。请使用它们作为您的引用。
// 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()
}
关于swift - 如何允许用户仅打开以编程方式构建的单个 uiSwitch 并存储开关的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52939010/