How do I save the state of the switch. I have a switch in a custom cell. I have a class for the custom cell. I am trying to save it from a view controller.
class Settings_Custom_Cell: UITableViewCell {
@IBOutlet weak var settingLabel: UILabel!
@IBOutlet weak var quickAddSwitch: UISwitch!
}
class SettingsCell {
var settingLabel: String
var settingValue: UISwitch
init(settingLabel: String, settingValue: UISwitch) {
self.settingLabel = settingLabel
self.settingValue = settingValue
}
}
import UIKit
class Settings: UIViewController, UITableViewDelegate, UITableViewDataSource {
var array: [String] = ["Quick Add"]
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return array.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = table.dequeueReusableCell(withIdentifier: "cell", for: indexPath as IndexPath)
cell.textLabel?.text = ""
cell.textLabel?.text = array[indexPath.row]
cell.textLabel?.numberOfLines = 0
cell.textLabel?.lineBreakMode = NSLineBreakMode.byWordWrapping
cell.textLabel?.font = UIFont.systemFont(ofSize: 20)
return cell
}
@IBOutlet weak var table: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
}
}
You declare
Settings_Custom_Cell
(a very odd name as Swift code), but you are not using it inSettings
. Have you really set up the storyboard forSettings
to use the custom classSettings_Custom_Cell
for the reuse identifiercell
?Yes, I setup storyboard. When I run the app, the switch displays fine but when I leave the view controller and return to the view controller the state of the switch is back to off.
OK, then can you explain why you are not using
Settings_Custom_Cell
in your code?