Three20 TTTableViewController commitEditingStyle
This post is mostly for my future reference. I found the documentation of the TTTableViewController’s commitEditingStyle to be lacking. Some of the comments on StackOverflow provide the right answer, but the code wasn’t posted so I struggled for a bit. Below is an example of the version I managed to get running.
TestDataSource.h:
@interface TestDataSource : TTListDataSource <uitableviewdatasource , TTModelDelegate> {} - (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section; - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath; - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath; @end</uitableviewdatasource>
TestDataSource.m
#import "TableTest.h" @implementation TableTest - (id)init { if (self = [super init]) { UIBarButtonItem *rightButton = [[[UIBarButtonItem alloc] initWithTitle:@"Edit" style:UIBarButtonSystemItemEdit target:self action:@selector(toggleEdit)] autorelease]; self.navigationItem.rightBarButtonItem = rightButton; self.title = @"Table Test"; NSArray *items = [NSArray arrayWithObjects: [TTTableTextItem itemWithText:@"Item 1"], [TTTableTextItem itemWithText:@"Item 2"], [TTTableTextItem itemWithText:@"Item 3"], nil ]; self.dataSource = [[TestDataSource alloc] initWithItems:items]; self.tableView.delegate = self; } return self; } - (void)toggleEdit { [self.tableView setEditing:!self.tableView.editing animated:YES]; NSString *label = self.tableView.editing == YES ? @"Done" : @"Edit"; self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:label style:UIBarButtonSystemItemEdit target:self action:@selector(toggleEdit)] autorelease]; } #pragma mark - #pragma mark TTTableViewDelegate - (void)tableView:(UITableView*)tableView touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event { [super touchesBegan:touches withEvent:event]; } - (void)tableView:(UITableView*)tableView touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event { [super touchesEnded:touches withEvent:event]; } @end @implementation TestDataSource - (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section { return [super tableView:table numberOfRowsInSection:section]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { return [super tableView:tableView cellForRowAtIndexPath:indexPath]; } - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { if (editingStyle == UITableViewCellEditingStyleDelete) { [self.items removeObjectAtIndex:indexPath.row]; NSArray *objects = [NSArray arrayWithObjects:indexPath, nil]; [tableView deleteRowsAtIndexPaths:objects withRowAnimation:UITableViewRowAnimationBottom]; } } @end