dimanche 5 février 2012

Generic Controller liste de sélection

J'ai un autre contrôleur de vue générique pour une utilisation dans une application de navigation basée. Celui-ci est pour avoir l'utilisateur de sélectionner un élément dans une liste, comme ceci:

Ce contrôleur fonctionne exactement comme les autres génériques que j'ai posté. Vous créez une instance, vous affectez tant que délégué et mettre en œuvre le protocole de déléguer, puis poussez sur la pile. La méthode du délégué unique sera utilisée pour vous informer d'une nouvelle sélection, si l'on a été faite. Donc, voici un exemple de création d'une instance et de le mettre sur la pile:

            SelectionListViewController *controller = [[SelectionListViewController alloc] init];
controller.delegate = self;
controller.list = myNSArrayWithAllPossibleSelections;
controller.initialSelection = theIndexOfTheCurrentlySelectedItem;
[self.navigationController pushViewController:controller animated:YES];
[controller release];

Ensuite, nous avons juste besoin de mettre en œuvre le:

- (void)rowChosen:(NSInteger)row
{
myObject.selection = row;
[self.tableView reloadData];
}

Tout le reste est géré pour vous. Voici le code:

SelectionListViewController.h
//
// SelectionListViewController.h
// iContractor
//
// Created by Jeff LaMarche on 2/18/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



SelectionListViewController.m
//
// SelectionListViewController.m
// iContractor
//
// Created by Jeff LaMarche on 2/18/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
{
initialSelection = -1;
return self;
}

- (void)viewDidLoad
{
[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];
}


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:UIBarButtonItemStylePlain
target:self
action:@selector(save)]
;
self.navigationItem.rightBarButtonItem = saveButton;
[saveButton release];

[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




Aucun commentaire: