diff --git a/Checklists.xcodeproj/project.pbxproj b/Checklists.xcodeproj/project.pbxproj index 4dc243b..68677b6 100644 --- a/Checklists.xcodeproj/project.pbxproj +++ b/Checklists.xcodeproj/project.pbxproj @@ -13,17 +13,19 @@ A81172CC1CD0660F00BA3196 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = A81172CB1CD0660F00BA3196 /* Assets.xcassets */; }; A81172CF1CD0660F00BA3196 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = A81172CD1CD0660F00BA3196 /* LaunchScreen.storyboard */; }; A81172D91CD0859800BA3196 /* ChecklistItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = A81172D81CD0859800BA3196 /* ChecklistItem.swift */; }; + A8961D4F1CD19E87006AFAF4 /* ItemDetailViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = A8961D4E1CD19E87006AFAF4 /* ItemDetailViewController.swift */; }; /* End PBXBuildFile section */ /* Begin PBXFileReference section */ A81172C11CD0660E00BA3196 /* Checklists.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Checklists.app; sourceTree = BUILT_PRODUCTS_DIR; }; A81172C41CD0660E00BA3196 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; - A81172C61CD0660E00BA3196 /* ChecklistViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChecklistViewController.swift; sourceTree = ""; }; + A81172C61CD0660E00BA3196 /* ChecklistViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; lineEnding = 0; path = ChecklistViewController.swift; sourceTree = ""; xcLanguageSpecificationIdentifier = xcode.lang.swift; }; A81172C91CD0660E00BA3196 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; A81172CB1CD0660F00BA3196 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; A81172CE1CD0660F00BA3196 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; A81172D01CD0660F00BA3196 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; A81172D81CD0859800BA3196 /* ChecklistItem.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ChecklistItem.swift; sourceTree = ""; }; + A8961D4E1CD19E87006AFAF4 /* ItemDetailViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; lineEnding = 0; path = ItemDetailViewController.swift; sourceTree = ""; xcLanguageSpecificationIdentifier = xcode.lang.swift; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -63,6 +65,7 @@ A81172CD1CD0660F00BA3196 /* LaunchScreen.storyboard */, A81172D01CD0660F00BA3196 /* Info.plist */, A81172D81CD0859800BA3196 /* ChecklistItem.swift */, + A8961D4E1CD19E87006AFAF4 /* ItemDetailViewController.swift */, ); path = Checklists; sourceTree = ""; @@ -140,6 +143,7 @@ buildActionMask = 2147483647; files = ( A81172C71CD0660E00BA3196 /* ChecklistViewController.swift in Sources */, + A8961D4F1CD19E87006AFAF4 /* ItemDetailViewController.swift in Sources */, A81172D91CD0859800BA3196 /* ChecklistItem.swift in Sources */, A81172C51CD0660E00BA3196 /* AppDelegate.swift in Sources */, ); @@ -291,6 +295,7 @@ A81172D51CD0660F00BA3196 /* Release */, ); defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; }; /* End XCConfigurationList section */ }; diff --git a/Checklists.xcodeproj/xcuserdata/mriddle.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist b/Checklists.xcodeproj/xcuserdata/mriddle.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist index 368f5e7..4048ea9 100644 --- a/Checklists.xcodeproj/xcuserdata/mriddle.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist +++ b/Checklists.xcodeproj/xcuserdata/mriddle.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist @@ -19,21 +19,5 @@ landmarkType = "5"> - - - - diff --git a/Checklists/Base.lproj/Main.storyboard b/Checklists/Base.lproj/Main.storyboard index 85c755e..af577b5 100644 --- a/Checklists/Base.lproj/Main.storyboard +++ b/Checklists/Base.lproj/Main.storyboard @@ -1,11 +1,11 @@ - + - + @@ -14,21 +14,30 @@ - - + + - + + + + + @@ -36,10 +45,115 @@ + + + + + + + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Checklists/ChecklistItem.swift b/Checklists/ChecklistItem.swift index 3032c44..4b8bb93 100644 --- a/Checklists/ChecklistItem.swift +++ b/Checklists/ChecklistItem.swift @@ -8,7 +8,7 @@ import Foundation -class ChecklistItem { +class ChecklistItem: NSObject { var text: String var checked: Bool diff --git a/Checklists/ChecklistViewController.swift b/Checklists/ChecklistViewController.swift index 97d1ddf..c4f1aab 100644 --- a/Checklists/ChecklistViewController.swift +++ b/Checklists/ChecklistViewController.swift @@ -8,7 +8,7 @@ import UIKit -class ChecklistViewController: UITableViewController { +class ChecklistViewController: UITableViewController, ItemDetailViewControllerDelegate { var items: Array = [ ChecklistItem(text: "Walk the dog", checked: false), ChecklistItem(text: "Brush teeth", checked: false), @@ -17,6 +17,22 @@ class ChecklistViewController: UITableViewController { ChecklistItem(text: "Eat ice cream", checked: false) ] + override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { + if segue.identifier == "AddItem" { + let navigationController = segue.destinationViewController as! UINavigationController + let controller = navigationController.topViewController as! ItemDetailViewController + + controller.delegate = self + } else if segue.identifier == "EditItem" { + let navigationController = segue.destinationViewController as! UINavigationController + let controller = navigationController.topViewController as! ItemDetailViewController + + controller.delegate = self + if let indexPath = tableView.indexPathForCell(sender as! UITableViewCell) { + controller.itemToEdit = items[indexPath.row] + } + } + } override func viewDidLoad() { super.viewDidLoad() @@ -28,6 +44,14 @@ class ChecklistViewController: UITableViewController { // Dispose of any resources that can be recreated. } + func addItem(item: ChecklistItem) { + let newRowIndex = items.count + items.append(item) + + let indexPath = NSIndexPath(forRow: newRowIndex, inSection: 0) + tableView.insertRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic) + } + override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return items.count } @@ -41,6 +65,13 @@ class ChecklistViewController: UITableViewController { return cell } + override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { + + items.removeAtIndex(indexPath.row) + tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic) + + } + override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { if let cell = tableView.cellForRowAtIndexPath(indexPath) { @@ -53,17 +84,37 @@ class ChecklistViewController: UITableViewController { tableView.deselectRowAtIndexPath(indexPath, animated: true) } + func configureTextForCell(cell: UITableViewCell, withChecklistItem item: ChecklistItem) { + let label = cell.viewWithTag(1000) as! UILabel + label.text = item.text + } + func configureCheckmarkForCell(cell: UITableViewCell, withChecklistItem item: ChecklistItem) { + let label = cell.viewWithTag(1001) as! UILabel if item.checked { - cell.accessoryType = .Checkmark + label.text = "√" } else { - cell.accessoryType = .None + label.text = "" } } - func configureTextForCell(cell: UITableViewCell, withChecklistItem item: ChecklistItem) { - let label = cell.viewWithTag(1000) as! UILabel - label.text = item.text + func addItemViewControllerDidCancel(controller: ItemDetailViewController) { + dismissViewControllerAnimated(true, completion: nil) + } + + func itemDetailViewController(controller: ItemDetailViewController, didFinishAddingItem item: ChecklistItem) { + dismissViewControllerAnimated(true, completion: nil) + addItem(item) + } + + func itemDetailViewController(controller: ItemDetailViewController, didFinishEditingItem item: ChecklistItem) { + if let index = items.indexOf(item) { + let indexPath = NSIndexPath(forRow: index, inSection: 0) + if let cell = tableView.cellForRowAtIndexPath(indexPath) { + configureTextForCell(cell, withChecklistItem: item) + } + } + dismissViewControllerAnimated(true, completion: nil) } } diff --git a/Checklists/ItemDetailViewController.swift b/Checklists/ItemDetailViewController.swift new file mode 100644 index 0000000..e52febc --- /dev/null +++ b/Checklists/ItemDetailViewController.swift @@ -0,0 +1,70 @@ +// +// ItemDetailViewController.swift +// Checklists +// +// Created by Matthew Riddle on 28/04/2016. +// Copyright © 2016 Matthew Riddle. All rights reserved. +// + +import Foundation +import UIKit + +protocol ItemDetailViewControllerDelegate: class { + func addItemViewControllerDidCancel(controller: ItemDetailViewController) + func itemDetailViewController(controller: ItemDetailViewController, didFinishAddingItem item: ChecklistItem) + func itemDetailViewController(controller: ItemDetailViewController, didFinishEditingItem item: ChecklistItem) +} + +class ItemDetailViewController: UITableViewController, UITextFieldDelegate { + + @IBOutlet weak var textField: UITextField! + @IBOutlet weak var doneBarButton: UIBarButtonItem! + + var itemToEdit: ChecklistItem? + + weak var delegate: ItemDetailViewControllerDelegate? + + override func viewDidLoad() { + super.viewDidLoad() + if let item = itemToEdit { + title = "Edit Item" + textField.text = item.text + doneBarButton.enabled = true + } + } + + @IBAction func done() { + + if let item = itemToEdit { + item.text = textField.text! + delegate?.itemDetailViewController(self, didFinishEditingItem: item) + } else { + let item = ChecklistItem(text: textField.text!, checked: false) + delegate?.itemDetailViewController(self, didFinishAddingItem: item) + } + + } + + @IBAction func cancel(sender: UIBarButtonItem) { + delegate?.addItemViewControllerDidCancel(self) + } + + override func viewWillAppear(animated: Bool) { + super.viewWillAppear(animated) + textField.becomeFirstResponder() + } + + override func tableView(tableView: UITableView, willSelectRowAtIndexPath indexPath: NSIndexPath) -> NSIndexPath? { + return nil + } + + func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool { + + let oldText: NSString = textField.text! + let newText: NSString = oldText.stringByReplacingCharactersInRange(range, withString: string) + + doneBarButton.enabled = (newText.length > 0) + + return true + } +} \ No newline at end of file