Skip to content

Instantly share code, notes, and snippets.

@derektu
Created February 15, 2013 08:43
Show Gist options
  • Save derektu/4959182 to your computer and use it in GitHub Desktop.
Save derektu/4959182 to your computer and use it in GitHub Desktop.
Code snippet to manage TableView rows
//--------------------------------------------------------------
// To add a row
//--------------------------------------------------------------
// TODO: append one row to self.dataArray
//
NSIndexPath* indexPath = [NSIndexPath indexPathForRow:self.dataArray.numberOfItems - 1 inSection:0];
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationAutomatic];
// Make sure the added row is visible
//
[self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
//--------------------------------------------------------------
// To delete a row at 'index'
//--------------------------------------------------------------
// TODO: remove the specified row from self.dataArray
//
// Update table view
//
NSIndexPath* indexPath = [NSIndexPath indexPathForRow:index inSection:0];
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationAutomatic];
//--------------------------------------------------------------
// To update a row at 'index'
//--------------------------------------------------------------
// TODO: update the row in self.dataArray
//
// Update table view
//
NSIndexPath* indexPath = [NSIndexPath indexPathForRow:index inSection:0];
[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationAutomatic];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment