Created
June 6, 2011 20:11
-
-
Save phillipuniverse/1010991 to your computer and use it in GitHub Desktop.
CheckMark Table Cells
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/Foundation.h> | |
@protocol ChangeRadiusDelegate | |
-(void) updateRadius:(int)radius; | |
@end | |
@interface ChangeRadius : TTTableViewController { | |
int _radius; | |
id<ChangeRadiusDelegate> _delegate; | |
} | |
@property (nonatomic, assign) int radius; | |
@property (nonatomic, assign) id<ChangeRadiusDelegate> delegate; | |
-(id) initWithRadius:(int)radius delegate:(id<ChangeRadiusDelegate>)delegate; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "ChangeRadius.h" | |
#import "CheckMarkTableItem.h" | |
#import "CheckMarkTableItemCell.h" | |
@interface ChangeRadiusDataSource : TTSectionedDataSource | |
@end | |
@implementation ChangeRadiusDataSource | |
-(Class) tableView:(UITableView *)tableView cellClassForObject:(id)object { | |
if([object isKindOfClass:[CheckMarkTableItem class]]) | |
return [CheckMarkTableItemCell class]; | |
else | |
return [super tableView:tableView cellClassForObject:object]; | |
} | |
@end | |
@interface ChangeRadius () | |
-(CheckMarkTableItemCell *) getSelectedCellFromSection:(int)section; | |
@end | |
@implementation ChangeRadius | |
@synthesize radius = _radius; | |
@synthesize delegate = _delegate; | |
-(id) initWithRadius:(int)radius delegate:(id <ChangeRadiusDelegate>)delegate{ | |
if(self = [super initWithStyle:UITableViewStyleGrouped]){ | |
_radius = radius; | |
_delegate = delegate; | |
NSArray *tableItems = [NSArray arrayWithObjects:[CheckMarkTableItem itemWithText:@"100 miles" state:CheckmarkNone value:[NSNumber numberWithInt:100]], | |
[CheckMarkTableItem itemWithText:@"200 miles" state:CheckmarkNone value:[NSNumber numberWithInt:200]], | |
[CheckMarkTableItem itemWithText:@"300 miles" state:CheckmarkNone value:[NSNumber numberWithInt:300]], | |
[CheckMarkTableItem itemWithText:@"400 miles" state:CheckmarkNone value:[NSNumber numberWithInt:400]], | |
[CheckMarkTableItem itemWithText:@"500 miles" state:CheckmarkNone value:[NSNumber numberWithInt:500]], | |
nil]; | |
//check the one with the current radius | |
for(CheckMarkTableItem *item in tableItems){ | |
if([(NSNumber *)item.value intValue] == _radius){ | |
item.state = CheckmarkChecked; | |
} | |
} | |
self.dataSource = [ChangeRadiusDataSource dataSourceWithItems:[NSArray arrayWithObject:tableItems] sections:[NSArray arrayWithObject:@"Farthest Distance"]]; | |
} | |
return self; | |
} | |
-(void) viewDidLoad{ | |
TTButton *submitButtonView = [TTButton buttonWithStyle:@"orangeToolbarButton:" title:@"Submit"]; | |
submitButtonView.userInteractionEnabled = YES; | |
[submitButtonView sizeToFit]; | |
[submitButtonView addTarget:self action:@selector(submitForm) forControlEvents:UIControlEventTouchUpInside]; | |
UIBarButtonItem *submitButton = [[[UIBarButtonItem alloc] initWithCustomView:submitButtonView] autorelease]; | |
[self.navigationItem setRightBarButtonItem:submitButton animated:YES]; | |
UIBarButtonItem *cancelButton = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(dismissModalViewController)] autorelease]; | |
[self.navigationItem setLeftBarButtonItem:cancelButton]; | |
self.title = @"Change Radius"; | |
} | |
-(void) submitForm{ | |
[self.delegate updateRadius:_radius]; | |
} | |
#pragma mark - | |
#pragma mark TTTableViewController | |
-(void) didSelectObject:(id)object atIndexPath:(NSIndexPath *)indexPath{ | |
if([object isKindOfClass:[CheckMarkTableItem class]]) { | |
CheckMarkTableItemCell *selectedCell = (CheckMarkTableItemCell *)[self.tableView cellForRowAtIndexPath:indexPath]; | |
//find and uncheck the currently selected cell | |
CheckMarkTableItemCell *cell = [self getSelectedCellFromSection:indexPath.section]; | |
cell.state = CheckmarkNone; | |
selectedCell.state = CheckmarkChecked; | |
_radius = [(NSNumber *)selectedCell.item.value intValue]; | |
[self.tableView deselectRowAtIndexPath:indexPath animated:YES]; | |
} else{ | |
[super didSelectObject:object atIndexPath:indexPath]; | |
} | |
} | |
-(CheckMarkTableItemCell *) getSelectedCellFromSection:(int)section{ | |
for(int i = 0; i < [self.tableView numberOfRowsInSection:section]; i++){ | |
CheckMarkTableItemCell *cell = (CheckMarkTableItemCell *)[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:section]]; | |
//it's possible that the cell is not on the screen which would make cellForRowAtIndexPath return nil. | |
//in this case, get the cell from the dataSource | |
if(!cell) { | |
cell = [self.dataSource tableView:self.tableView objectForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:section]]; | |
} | |
if(cell.item.state == CheckmarkChecked){ | |
return cell; | |
} | |
} | |
TTDPRINT(@"Didn't find a checked cell"); | |
//shouldn't ever happen | |
return nil; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/Foundation.h> | |
typedef enum CheckmarkState { | |
CheckmarkNone = NO, | |
CheckmarkChecked = YES | |
} CheckmarkState; | |
@interface CheckMarkTableItem : TTTableTextItem { | |
CheckmarkState state; | |
NSObject *value; | |
} | |
@property (nonatomic) CheckmarkState state; | |
@property (nonatomic, retain) NSObject *value; | |
+ (id)itemWithText:(NSString *)text state:(CheckmarkState)state value:(NSObject *)value;; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "CheckMarkTableItem.h" | |
@implementation CheckMarkTableItem | |
@synthesize state; | |
@synthesize value; | |
+ (id)itemWithText:(NSString *)text state:(CheckmarkState)state value:(NSObject *)value;{ | |
CheckMarkTableItem *item = [[[self alloc] init] autorelease]; | |
item.text = text; | |
item.state = state; | |
item.value = value; | |
return item; | |
} | |
-(void) dealloc{ | |
[super dealloc]; | |
TT_RELEASE_SAFELY(value); | |
} | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/Foundation.h> | |
#import "CheckMarkTableItem.h" | |
@interface CheckMarkTableItemCell : TTTableTextItemCell { | |
CheckMarkTableItem *item; | |
} | |
@property (nonatomic, retain) CheckMarkTableItem *item; | |
@property (nonatomic, assign) CheckmarkState state; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@implementation CheckMarkTableItemCell | |
@synthesize item; | |
@dynamic state; | |
- (void)setObject:(id)object { | |
// _item is defined in TTTableTextItemCell. | |
if(_item != object) { | |
[super setObject:object]; | |
self.item = object; | |
self.selectionStyle = TTSTYLEVAR(tableSelectionStyle); | |
// Set the accessoryType | |
if([self.item state]) | |
self.accessoryType = UITableViewCellAccessoryCheckmark; | |
else | |
self.accessoryType = UITableViewCellAccessoryNone; | |
} | |
} | |
- (void)setState:(CheckmarkState)state { | |
self.item.state = state; | |
if(state) | |
self.accessoryType = UITableViewCellAccessoryCheckmark; | |
else | |
self.accessoryType = UITableViewCellAccessoryNone; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment