mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-28 04:24:20 +00:00
Project organization README.md WIP QueryBuilder documentation README.md WIP QueryBuilder documentation WIP QueryBuilder documentation TODO.md
81 lines
2.8 KiB
Swift
81 lines
2.8 KiB
Swift
//
|
|
// MasterViewController.swift
|
|
// iOS
|
|
//
|
|
// Created by Gwendal Roué on 08/07/2015.
|
|
// Copyright © 2015 Gwendal Roué. All rights reserved.
|
|
//
|
|
|
|
import UIKit
|
|
|
|
class MasterViewController: UITableViewController {
|
|
|
|
var detailViewController: DetailViewController? = nil
|
|
var persons = [Person]()
|
|
|
|
|
|
override func viewDidLoad() {
|
|
super.viewDidLoad()
|
|
// Do any additional setup after loading the view, typically from a nib.
|
|
self.navigationItem.leftBarButtonItem = self.editButtonItem()
|
|
|
|
if let split = self.splitViewController {
|
|
let controllers = split.viewControllers
|
|
self.detailViewController = (controllers[controllers.count-1] as! UINavigationController).topViewController as? DetailViewController
|
|
}
|
|
}
|
|
|
|
override func viewWillAppear(animated: Bool) {
|
|
persons = dbQueue.inDatabase { db in
|
|
Person.fetchAll(db, "SELECT * FROM persons ORDER BY firstName, lastName")
|
|
}
|
|
self.clearsSelectionOnViewWillAppear = self.splitViewController!.collapsed
|
|
super.viewWillAppear(animated)
|
|
}
|
|
|
|
// MARK: - Segues
|
|
|
|
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
|
|
if segue.identifier == "showDetail" {
|
|
if let indexPath = self.tableView.indexPathForSelectedRow {
|
|
let person = persons[indexPath.row]
|
|
let controller = (segue.destinationViewController as! UINavigationController).topViewController as! DetailViewController
|
|
controller.person = person
|
|
controller.navigationItem.leftBarButtonItem = self.splitViewController?.displayModeButtonItem()
|
|
controller.navigationItem.leftItemsSupplementBackButton = true
|
|
}
|
|
}
|
|
}
|
|
|
|
// MARK: - Table View
|
|
|
|
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
|
|
return 1
|
|
}
|
|
|
|
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
|
return persons.count
|
|
}
|
|
|
|
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
|
|
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
|
|
|
|
let person = persons[indexPath.row]
|
|
cell.textLabel!.text = person.fullName
|
|
return cell
|
|
}
|
|
|
|
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
|
|
let person = persons[indexPath.row]
|
|
try! dbQueue.inTransaction { db in
|
|
try person.delete(db)
|
|
return .Commit
|
|
}
|
|
persons.removeAtIndex(indexPath.row)
|
|
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
|
|
}
|
|
|
|
|
|
}
|
|
|