This file contains hidden or 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
class Product : NSObject, NSCoding { | |
let nameKey = "name" | |
let qtyKey = "qty" | |
var name : String = "" | |
var qty : Int = 0 | |
required init(coder aDecoder: NSCoder) { | |
if let name = aDecoder.decodeObjectForKey(nameKey) as? String { |
This file contains hidden or 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
class ProductManager { | |
var products : [Product] = [] | |
var productsKey : String = "products" | |
let userDefaults = NSUserDefaults.standardUserDefaults() | |
func saveProductsToDefaults() { | |
let productsKeyedArchive = NSKeyedArchiver.archivedDataWithRootObject(self.products) |
This file contains hidden or 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 tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool { | |
return true | |
} | |
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { | |
if editingStyle == UITableViewCellEditingStyle.Delete { | |
if let removedCell = tableView.cellForRowAtIndexPath(indexPath) as? ProductCell { | |
let removedProduct = Product(name: removedCell.productNameLabel.text!, qty: 1) |
This file contains hidden or 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 removeProduct(product: Product) { | |
let index = self.products.indexOf { (p : Product) -> Bool in | |
return product.name == p.name | |
} | |
if index != nil { | |
self.products.removeAtIndex(index!) | |
} | |
This file contains hidden or 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 SWTableViewCell | |
class ProductCell : SWTableViewCell { | |
@IBOutlet weak var productNameLabel: UILabel! | |
} |
This file contains hidden or 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
// Allow just one cell's utility button to be open at once | |
func swipeableTableViewCellShouldHideUtilityButtonsOnSwipe(cell: SWTableViewCell!) -> Bool { | |
return true | |
} | |
// Determines which swipe directions are allowed per cell | |
func swipeableTableViewCell(cell: SWTableViewCell!, canSwipeToState state: SWCellState) -> Bool { | |
switch state { | |
case SWCellState.CellStateLeft: |
This file contains hidden or 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: TableViewDataSource Delegate Methods | |
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { | |
if let cell = tableView.dequeueReusableCellWithIdentifier("Product Cell") as? ProductCell { | |
cell.productNameLabel.text = self.shoppingListManager.productAtIndex(indexPath.row).name | |
// Set up SWTableViewCell utility buttons | |
cell.delegate = self | |
cell.leftUtilityButtons = self.leftButtons() |
This file contains hidden or 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
enum LeftUtilityButtons: Int { | |
case Purchase | |
case Favorite | |
} | |
enum RightUtilityButtons: Int { | |
case Delete | |
} | |
// MARK: SWTableViewCell Delegate Methods |
This file contains hidden or 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: ReorderTableView Delegate Methods | |
func saveObjectAndInsertBlankRowAtIndexPath(indexPath: NSIndexPath!) -> AnyObject! { | |
let p = self.shoppingListManager.productAtIndex(indexPath.row) | |
self.shoppingListManager.replaceProductAtIndex(indexPath.row, withProduct: Product(name: "", qty: 0)) | |
return p | |
} | |
func moveRowAtIndexPath(fromIndexPath: NSIndexPath!, toIndexPath: NSIndexPath!) { | |
let p = self.shoppingListManager.productAtIndex(fromIndexPath.row) |
This file contains hidden or 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 swipeableTableViewCell(cell: SWTableViewCell!, didTriggerRightUtilityButtonWithIndex index: Int) { | |
switch index { | |
case RightUtilityButtons.Delete.rawValue: | |
if let indexPath = self.shoppingListTableView.indexPathForCell(cell) as NSIndexPath! { | |
self.shoppingListTableView.beginUpdates() | |
self.shoppingListTableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Bottom) | |
self.shoppingListManager.removeProductAtIndex(indexPath.row) | |
self.shoppingListTableView.endUpdates() | |
} | |
default: |
OlderNewer