Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save kevinkirkup/3899138 to your computer and use it in GitHub Desktop.
Save kevinkirkup/3899138 to your computer and use it in GitHub Desktop.
UITableView Delete
// From: http://useyourloaf.com/blog/2010/10/04/swiping-to-delete-rows-from-a-table.html
/**
* Perform anything that needs to be done before the Delete button is shown
*/
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView
editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
/* Do Stuff */
// Example: Disable the Delete button if this is the last row in the table.
NSUInteger row = [indexPath row];
NSUInteger count = [posts count];
if (row < count) {
return UITableViewCellEditingStyleDelete;
} else {
return UITableViewCellEditingStyleNone;
}
}
/**
* Perform the delete operation on the table view
*/
- (void)tableView:(UITableView *)tableView
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath {
/* Do your delete work here */
// Example: Delete the post
NSUInteger row = [indexPath row];
NSUInteger count = [posts count];
if (row < count) {
[posts removeObjectAtIndex:row];
}
}
/**
* Update the tableview after the change
*/
- (void)tableView:(UITableView *)tableView
didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath {
/* Update the table view and any associated views */
// Example: Update the Title bar based on the number of posts
[self updateViewTitle];
[tableView reloadData];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment