Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions Settings/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

12 changes: 8 additions & 4 deletions Settings/SettingsMiniProject/SettingsController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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]
}
9 changes: 4 additions & 5 deletions Settings/SettingsMiniProject/SettingsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand All @@ -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()
}

}