From d0d57c5d90e24bfd00f584dde606e2636f7db3bb Mon Sep 17 00:00:00 2001 From: Aaron Martinez Date: Tue, 14 Nov 2017 11:15:18 -0700 Subject: [PATCH] Change code to update model's isSet property in Model Controller instead of in the VC --- Settings/README.md | 7 ++++--- .../SettingsMiniProject/SettingsController.swift | 12 ++++++++---- .../SettingsMiniProject/SettingsViewController.swift | 9 ++++----- 3 files changed, 16 insertions(+), 12 deletions(-) diff --git a/Settings/README.md b/Settings/README.md index 17932c4..6efd770 100644 --- a/Settings/README.md +++ b/Settings/README.md @@ -14,8 +14,9 @@ Before starting, make sure everyone has the images they need. 5. Set up your model controller with mock data. 6. Implement your table view data source functions but leave a comment where you should be updating the cell's subviews. 7. Create a 'setting' computed property in your custom cell class and set it in your table view data source function. Include in the computed property a change in cell background color based on the setting's isSet property (that way you can tell what the model is storing for isSet even after tapping the switch). - * Make sure everyone can run their app and show the four mock settings in the tableview. Explain that although the switch is moving, the background color is not changing which means that we aren't actually changing the model object, which we need to do. +* Make sure everyone can run their app and show the four mock settings in the tableview. Explain that although the switch is moving, the background color is not changing which means that we aren't actually changing the model object, which we need to do. 8. Begin to explain the delegate pattern and review how protocols work. It might be useful to update the model in the action and ask why that isn't appropriate (views shouldn't interact with models). A good analogy is an outfielder throwing the ball to home plate - it's possible but the ball will bounce and so it's more effective to throw to an infielder who will throw home. 9. Make your custom delegate, add a property to the cell of the delegate type, and call your delegate function in your action. -10. Make your view controller conform to the delegate protocol and implement the body of the delegate function by setting the 'isSet' property on the 'setting' instance. - * Run and make sure everyone sees how the background color of the cell now changes because we have created a chain of calls that end in updating the model object. Make sure everyone's project is working. +10. Make your view controller conform to the delegate protocol and implement the body of the delegate function by calling a function in the Model Controller which will set the 'isSet' property on the 'setting' instance. +* Run and make sure everyone sees how the background color of the cell now changes because we have created a chain of calls that end in updating the model object. Make sure everyone's project is working. + diff --git a/Settings/SettingsMiniProject/SettingsController.swift b/Settings/SettingsMiniProject/SettingsController.swift index fa23907..7e019a1 100644 --- a/Settings/SettingsMiniProject/SettingsController.swift +++ b/Settings/SettingsMiniProject/SettingsController.swift @@ -17,9 +17,13 @@ class SettingsController { let apps = Setting(name: "Apps", isSet: true, image: UIImage(named: "apps")) let books = Setting(name: "Books", isSet: false, image: UIImage(named: "books")) let updates = Setting(name: "Updates", isSet: false, image: UIImage(named: "apps")) - - mySettings = [music, apps, books, updates] + + mySettings = [music, apps, books, updates] } - - var mySettings: [Setting] + + func toggleIsSet(setting: Setting) { + setting.isSet = !setting.isSet + } + + var mySettings: [Setting] } diff --git a/Settings/SettingsMiniProject/SettingsViewController.swift b/Settings/SettingsMiniProject/SettingsViewController.swift index a454dcd..69b66bc 100644 --- a/Settings/SettingsMiniProject/SettingsViewController.swift +++ b/Settings/SettingsMiniProject/SettingsViewController.swift @@ -11,16 +11,16 @@ import UIKit class SettingsViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, SettingTableViewCellDelegate { @IBOutlet weak var tableView: UITableView! - + func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return SettingsController.shared.mySettings.count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "settingCell") as? SettingTableViewCell - + let setting = SettingsController.shared.mySettings[(indexPath as NSIndexPath).row] - cell?.setting = setting + cell?.setting = setting cell?.delegate = self return cell ?? UITableViewCell() } @@ -29,9 +29,8 @@ class SettingsViewController: UIViewController, UITableViewDelegate, UITableView guard let setting = cell.setting, let cellIndexPath = tableView.indexPath(for: cell) else {return} tableView.beginUpdates() - setting.isSet = selected + SettingsController.shared.toggleIsSet(setting: setting) tableView.reloadRows(at: [cellIndexPath], with: .automatic) tableView.endUpdates() } - }