UITableView Editing
Set the editing property on UITableView to YES to enable editing mode:
To enable this for the user, we add a headerView at the top of the table and add buttons to it to Add New Item and Enable/Disable Editing:
@interface MyViewController : UITableViewController { IBOutlet UIView *headerView; } - (UIView *)headerView; - (IBAction)addNewItem:(id)sender; - (IBAction)enableDisableEditing:(id)sender; @end
File > New > New File > iOS > User Interface, and choose Empty. Name this file "HeaderView". Change the File Owner object of this XIB file to MyViewController above.
Drag a new UIView on to the empty canvas. Drag 2 buttons on to the UIView, labelling them "Add" and "Edit". Connect these to relevant IBActions on the File Owner (MyViewController).
In the implementation file:
- (UIView *)headerView { if (!headerView) { [[NSBundle mainBundle] loadNibNamed:@"HeaderView" owner:self options:nil]; } return headerView; }
The first time headerView is called on MyViewController, it will load the HeaderView XIB file created above. The Add and Edit buttons inside will send messages to ItemsViewController when pressed.
Implement these methods in the implementation file:
- (UIView *)tableView: (UITableView *)tableView viewForHeaderInSection:(NSInteger)section { return [self headerView]; } - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return [[self headerView] bounds].size.height; }
These are both required for a header view.
UITableView sends messages to its delegate ItemsViewController. As soon as headerView is needed it will be loaded from the XIB file.
To load a XIB file manually, call loadNibNamed:owner:options.
Implement the methods for the buttons:
- (IBAction)toggleEditingMode:(id)sender { if ([self isEditing]) { [sender setTitle:@"Edit" forState:UIControlStateNormal]; [self setEditing:NO animated:YES]; } else { [sender setTitle:@"Done" forState:UIControlStateNormal]; [self setEditing:YES animated:YES]; } }
Adding Rows
- (IBAction)addNewItem: (id)sender { Record *newRecord = [recordStore createItem]; int lastRow = [[recordStore allItems] indexOfObject:newRecord];
NSIndexPath *path = [NSIndexPath indexPathForRow:lastRowinSection:0];
[[self tableView] insertRowsAtIndexPaths: [NSArray arrayWithObject:path] withRowAnimation:UITableViewRowAnimationTop]; }
Removing Rows
To delete a cell, remove it from the UITableView and then from the model.
In the interface for our Record object:
- (void)removeItem:(Record *)record;
Then in the item store:
- (void)removeItem:(Record *)record { [allItems removeObjectByIdenticalTo:record]; }
Then in MyViewController:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle rowRowAtIndexPath:(NSIndexPath *)indexPath { if (editingStyle == UITableViewCellEditingStyleDelete) { NSArray records = [ItemStore allItems]; Record *record = [records objectAtIndex:[indexPath row]]; [ItemStore removeItem:record]; [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; } }
Moving Rows
Declare and implement the following method in the item store:
- (void)moveItemAtIndex:(int)from toIndex:(int)to { if (from == to) { return; } Record *record = [allItems objectAtIndex:from]; [allItems removeObjectAtIndex:from]; [allItems insertObject:record atIndex:to]; }
Then in MyViewController:
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath { [ItemStore moveItemAtIndex:[sourceIndexPath row] toIndex:[destinationIndexPath row]]; } }

















