Skip to content

Instantly share code, notes, and snippets.

View ctrevarthen's full-sized avatar

Chris Trevarthen ctrevarthen

View GitHub Profile
@ctrevarthen
ctrevarthen / gist:f230f5b1603f4c8e97f2
Last active November 19, 2015 03:40
Encoding and Decoding for NSUserDefaults
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 {
@ctrevarthen
ctrevarthen / gist:86470886ec5aeb1650f3
Last active November 19, 2015 03:40
Saving and loading products with NSUserDefaults
class ProductManager {
var products : [Product] = []
var productsKey : String = "products"
let userDefaults = NSUserDefaults.standardUserDefaults()
func saveProductsToDefaults() {
let productsKeyedArchive = NSKeyedArchiver.archivedDataWithRootObject(self.products)
@ctrevarthen
ctrevarthen / gist:9a4151e458eba6d24763
Last active November 19, 2015 03:41
Swipe to delete
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)
@ctrevarthen
ctrevarthen / gist:ab39ba7cdc848cff54dc
Last active November 19, 2015 03:41
Shopping List Manager - Remove Product
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!)
}
@ctrevarthen
ctrevarthen / gist:27f780533be13826e7ee
Last active November 19, 2015 03:39
ShopQuick - ProductCell with SWTableViewCell
import UIKit
import SWTableViewCell
class ProductCell : SWTableViewCell {
@IBOutlet weak var productNameLabel: UILabel!
}
@ctrevarthen
ctrevarthen / gist:c82937dd90ec7a9cef11
Last active November 19, 2015 03:39
ShopQuick - ShoppingListVC with SWTableViewCell Delegate methods - swipe control
// 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:
@ctrevarthen
ctrevarthen / gist:2f42f86d155da02f1157
Last active November 19, 2015 03:39
ShopQuick: SWTableViewCell buttons
// 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()
@ctrevarthen
ctrevarthen / gist:4f3e15c0635847e3e158
Last active November 19, 2015 03:40
ShopQuick - ShoppingListVC with SWTableViewCell Delegate methods - actions
enum LeftUtilityButtons: Int {
case Purchase
case Favorite
}
enum RightUtilityButtons: Int {
case Delete
}
// MARK: SWTableViewCell Delegate Methods
@ctrevarthen
ctrevarthen / gist:58fb6528572e2eebd7af
Last active November 19, 2015 03:39
BVReorderTableView Delegate Methods
// 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)
@ctrevarthen
ctrevarthen / gist:1a999d68c8f2de64fa43
Last active November 19, 2015 03:40
ShopQuick: Delete with utility
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: