Created
February 22, 2009 19:20
-
-
Save spjwebster/68574 to your computer and use it in GitHub Desktop.
A generic selection list UITableViewController subclass, revised from Jeff LaMarche's original.
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
// | |
// SelectionListViewController.h | |
// | |
// Created by Jeff LaMarche on 2/18/09. | |
// Revised by Steve Webster on 2/22/09. | |
// | |
#import <UIKit/UIKit.h> | |
@protocol SelectionListViewControllerDelegate <NSObject> | |
@required | |
- (void)rowChosen:(NSInteger)row; | |
@end | |
@interface SelectionListViewController : UITableViewController { | |
NSArray *list; | |
NSIndexPath *lastIndexPath; | |
NSInteger initialSelection; | |
id <SelectionListViewControllerDelegate> delegate; | |
} | |
@property (nonatomic, retain) NSIndexPath *lastIndexPath; | |
@property (nonatomic, retain) NSArray *list; | |
@property NSInteger initialSelection; | |
@property (nonatomic, assign) id <SelectionListViewControllerDelegate> 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
// | |
// SelectionListViewController.m | |
// | |
// Created by Jeff LaMarche on 2/18/09. | |
// Revised by Steve Webster on 2/22/09. | |
// | |
#import "SelectionListViewController.h" | |
@implementation SelectionListViewController | |
@synthesize list; | |
@synthesize lastIndexPath; | |
@synthesize initialSelection; | |
@synthesize delegate; | |
-(IBAction)cancel { | |
[self.navigationController popViewControllerAnimated:YES]; | |
} | |
-(IBAction)save { | |
[self.delegate rowChosen:[lastIndexPath row]]; | |
[self.navigationController popViewControllerAnimated:YES]; | |
} | |
#pragma mark - | |
- (id)initWithStyle:(UITableViewStyle)style { | |
if ( self = [super initWithStyle:style] ) { | |
initialSelection = -1; | |
} | |
return self; | |
} | |
- (void)viewDidLoad { | |
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] | |
initWithTitle:NSLocalizedString(@"Cancel", @"Cancel - for button to cancel changes") | |
style:UIBarButtonItemStylePlain | |
target:self | |
action:@selector(cancel)]; | |
self.navigationItem.leftBarButtonItem = cancelButton; | |
[cancelButton release]; | |
UIBarButtonItem *saveButton = [[UIBarButtonItem alloc] | |
initWithTitle:NSLocalizedString(@"Save", @"Save - for button to save changes") | |
style:UIBarButtonItemStyleDone | |
target:self | |
action:@selector(save)]; | |
self.navigationItem.rightBarButtonItem = saveButton; | |
[saveButton release]; | |
[super viewDidLoad]; | |
} | |
- (void)viewWillAppear:(BOOL)animated { | |
// Check to see if user has indicated a row to be selected, and set it | |
if (initialSelection > - 1 && initialSelection < [list count]) { | |
NSUInteger newIndex[] = {0, initialSelection}; | |
NSIndexPath *newPath = [[NSIndexPath alloc] initWithIndexes:newIndex length:2]; | |
self.lastIndexPath = newPath; | |
[newPath release]; | |
} | |
[self.tableView reloadData]; | |
[super viewWillAppear:animated]; | |
} | |
- (void)dealloc { | |
[list release]; | |
[lastIndexPath release]; | |
[super dealloc]; | |
} | |
#pragma mark - | |
#pragma mark Tableview methods | |
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { | |
return [list count]; | |
} | |
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { | |
static NSString *SelectionListCellIdentifier = @"SelectionListCellIdentifier"; | |
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SelectionListCellIdentifier]; | |
if (cell == nil) { | |
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:SelectionListCellIdentifier] autorelease]; | |
} | |
NSUInteger row = [indexPath row]; | |
NSUInteger oldRow = [lastIndexPath row]; | |
cell.text = [list objectAtIndex:row]; | |
cell.accessoryType = (row == oldRow && lastIndexPath != nil) ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone; | |
return cell; | |
} | |
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { | |
int newRow = [indexPath row]; | |
int oldRow = [lastIndexPath row]; | |
if (newRow != oldRow) { | |
UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath]; | |
newCell.accessoryType = UITableViewCellAccessoryCheckmark; | |
UITableViewCell *oldCell = [tableView cellForRowAtIndexPath: lastIndexPath]; | |
oldCell.accessoryType = UITableViewCellAccessoryNone; | |
lastIndexPath = indexPath; | |
} | |
[tableView deselectRowAtIndexPath:indexPath animated:YES]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment