Reputation: 1740
I have the following custom UITableViewCell
:
I want my view controller to be notified when somebody flips a cell's switch in order to update my model. I've tried using the table view's delegate methods (didSelect
, didFinishEditing
, didHighlight
, etc.) but none of them are called upon this action. Is there any way to do what I want to do? Somebody please help.
Upvotes: 1
Views: 210
Reputation: 17854
To update your model when somebody flips a cell's switch you need:
Assign cell's @IBAction func onSwitched(_ sender: UISwitch)
as UISwitch Value Changed
listener as shown on this screenshot
Attach color model to cell
cell.myColorModel = myColorModels[indexPath.row]
In @IBAction func onSwitched(_ sender: UISwitch)
simply change selected
property in model
@IBAction func onSwitched(_ sender: UISwitch) {
myColorModel.selected = sender.isOn
}
FULL SOURCE CODE
class MyColorModel {
var title: String!
var color: UIColor!
var selected: Bool = false
init(title: String, color: UIColor) {
self.title = title
self.color = color
}
}
class MyColorCell: UITableViewCell {
@IBOutlet weak var colorTitle: UILabel!
@IBOutlet weak var colorImage: UIImageView!
@IBOutlet weak var colorSwitch: UISwitch!
var myColorModel: MyColorModel! {
didSet {
colorTitle.text = myColorModel.title
colorImage.backgroundColor = myColorModel.color
colorSwitch.isOn = myColorModel.selected
}
}
@IBAction func onSwitched(_ sender: UISwitch) {
myColorModel.selected = sender.isOn
}
}
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
fileprivate var myColorModels = [MyColorModel(title: "Red", color: UIColor.red),
MyColorModel(title: "Green", color: UIColor.green),
MyColorModel(title: "Blue", color: UIColor.blue)]
@IBAction func onColorsCheck(_ sender: AnyObject) {
for myColorModel in myColorModels {
print("color \(myColorModel.title) \((myColorModel.selected) ? "is checked":"is not checked")")
}
}
// MARK: - UITableView datasource & delegate
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return myColorModels.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "MyColorCell") as! MyColorCell
cell.myColorModel = myColorModels[indexPath.row]
return cell
}
}
Upvotes: 0
Reputation: 82776
actually your UISwitch has added to accessoryView
of UITableViewCell
, so do like on cellforRowAtIndex
var switchView = UISwitch(frame: CGRect.zero)
aCell.accessoryView = switchView
lightSwitch.tag = indexPath.row
switchView.setOn(false, animated: false)
switchView.addTarget(self, action: #selector(switchChanged(_:), for: .valueChanged)
and get the action of UISwitch
as
func switchChanged(_ sender: UISwitch) {
print("which switch is \(sender.tag)")
print("The switch is \(sender?.on ? "ON" : "OFF")")
}
Upvotes: 5