Skip to content

Instantly share code, notes, and snippets.

@ctrevarthen
Last active November 19, 2015 03:41
Show Gist options
  • Save ctrevarthen/9a4151e458eba6d24763 to your computer and use it in GitHub Desktop.
Save ctrevarthen/9a4151e458eba6d24763 to your computer and use it in GitHub Desktop.
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)
self.shoppingListManager.removeProduct(removedProduct)
removedCell.endEditing(false)
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Fade)
}
}
}
func tableView(tableView: UITableView, willBeginEditingRowAtIndexPath indexPath: NSIndexPath) {
if let cell = tableView.cellForRowAtIndexPath(indexPath) as? ProductCell {
cell.startEditing()
}
}
func tableView(tableView: UITableView, didEndEditingRowAtIndexPath indexPath: NSIndexPath) {
if let cell = tableView.cellForRowAtIndexPath(indexPath) as? ProductCell {
cell.endEditing(false)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment