Eightlines Creations

Experiments in Physical Computing

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
 
September 11th 2009
Tags: Found

0 Comments

TGIMBOEJ

The Great Internet Migratory Box of Electronics Junk (TGIMBOEJ) is a box. It's a box that arrives on your doorstep after you drop your name on a wiki page and wait a good deal of time. It's a box that has electronics junk inside and it works off the premise ...
August 9th 2009
Tags: Ideas

0 Comments

Power Monitor Packet

As Hugo helpfully commented in a previous post, here is the link to the Power Monitor Patent. The specific page I'm linking to is the Packet model. Lots of reading to do here, but its pretty interesting. One of the things I find most interesting about this patent is the citations ...
July 26th 2009
Tags: Ideas

0 Comments

Building a Scooter Bike for Kids

Ages ago I bookmarked a design posted on Make for a Scooter Bike, as I intended on building one when my daughter got old enough. On a recent trip to the surplus store I picked up a wheelset for $3 and figured what better time to start than now. I ...
June 10th 2009
Tags: Ideas

0 Comments

Arduino Packet Analyzer

Following the previous success where we managed to see the transmissions from the Black & Decker Power Monitor, Andrew Kilpatrick and I went about trying to capture the packets. Without an access to an oscilloscope with packet capture capabilities we built our own with an Arduino. Essentially, the code waits for ...
June 8th 2009
Tags: Ideas, Power Monitor

0 Comments

Black & Decker Power Monitor Hack

Black & Decker makes an interesting product called the Power Monitor. The monitor straps on to your hydro meter and reports the revolutions of a spinning disc or blinks of an IR LED depending which model of meter you have. It then broadcasts that reading to a handheld unit inside ...

Search

The archives run deep. Feel free to search older content using topic keywords.