This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// MARK: - Navigation | |
extension PersonsViewController { | |
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { | |
if segue.identifier == "NewPerson" { | |
let navigationController = segue.destinationViewController as! UINavigationController | |
let controller = navigationController.viewControllers.first as! PersonEditionViewController | |
controller.title = "New Person" | |
controller.person = Person(name: "", score: 0) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension PersonEditionViewController { | |
func applyChanges() { | |
person.name = nameTextField.text | |
person.score = scoreTextField.text.flatMap { Int($0) } ?? 0 | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// MARK: - Form | |
extension PersonEditionViewController: UITextFieldDelegate { | |
override func viewWillAppear(animated: Bool) { | |
super.viewWillAppear(animated) | |
nameTextField.becomeFirstResponder() | |
} | |
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
class PersonEditionViewController: UITableViewController { | |
var person: Person! | |
@IBOutlet private weak var nameCell: UITableViewCell! | |
@IBOutlet private weak var nameTextField: UITextField! | |
@IBOutlet private weak var scoreCell: UITableViewCell! | |
@IBOutlet private weak var scoreTextField: UITextField! | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { | |
// Delete the person | |
let person = personsController.recordAtIndexPath(indexPath) | |
try! person.delete(dbQueue) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// MARK: - FetchedRecordsControllerDelegate | |
extension PersonsViewController : FetchedRecordsControllerDelegate { | |
func controllerWillChangeRecords<T>(controller: FetchedRecordsController<T>) { | |
tableView.beginUpdates() | |
} | |
func controller<T>(controller: FetchedRecordsController<T>, didChangeRecord record: T, withEvent event:FetchedRecordsEvent) { | |
switch event { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func setupDatabase() { | |
// That "collation" helps us compare person names in a localized case insensitive fashion | |
let collation = DatabaseCollation.localizedCaseInsensitiveCompare | |
try! dbQueue.execute( | |
"CREATE TABLE persons (" + | |
"id INTEGER PRIMARY KEY, " + | |
"name TEXT NOT NULL COLLATE \(collation.name), " + | |
"score INTEGER NOT NULL " + | |
")") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// MARK: - UITableViewDataSource | |
extension FetchedRecordsControllerDemoViewController { | |
override func numberOfSectionsInTableView(tableView: UITableView) -> Int { | |
return personsController.sections.count | |
} | |
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { | |
return personsController.sections[section].numberOfRecords |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
import GRDB | |
class PersonsViewController: UITableViewController { | |
var personsController: FetchedRecordsController<Person>! | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// The persons, sorted by score then by name |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let person = Person(name: "Arthur", score: 100) | |
try person.insert(dbQueue) | |
let persons = Person.fetchAll(dbQueue) // [Persons] |