cocoaTouch/LevelViewController.m
changeset 3340 96dd168b080b
parent 3339 d558bc5a73c5
child 3352 ac5d14a35482
equal deleted inserted replaced
3339:d558bc5a73c5 3340:96dd168b080b
     4 //
     4 //
     5 //  Created by Vittorio on 02/04/10.
     5 //  Created by Vittorio on 02/04/10.
     6 //  Copyright 2010 __MyCompanyName__. All rights reserved.
     6 //  Copyright 2010 __MyCompanyName__. All rights reserved.
     7 //
     7 //
     8 
     8 
     9 #import "HogHatViewController.h"
     9 #import "LevelViewController.h"
    10 #import "CommodityFunctions.h"
    10 #import "CommodityFunctions.h"
    11 
    11 
    12 
    12 
    13 @implementation LevelViewController
    13 @implementation LevelViewController
    14 @synthesize teamDictionary, levelArray, levelSprites, lastIndexPath;
    14 @synthesize teamDictionary, levelArray, levelSprites, lastIndexPath;
    23 #pragma mark View lifecycle
    23 #pragma mark View lifecycle
    24 - (void)viewDidLoad {
    24 - (void)viewDidLoad {
    25     [super viewDidLoad];
    25     [super viewDidLoad];
    26 
    26 
    27     NSArray *array = [[NSArray alloc] initWithObjects:
    27     NSArray *array = [[NSArray alloc] initWithObjects:
    28                              NSLocalizedString(@"Human",@""),
    28                       NSLocalizedString(@"Human",@""),
    29                              NSLocalizedString(@"Weaky",@""),
    29                       NSLocalizedString(@"Weaky",@""),
    30                              NSLocalizedString(@"Average",@""),
    30                       NSLocalizedString(@"Average",@""),
    31                              NSLocalizedString(@"Bully",@""),
    31                       NSLocalizedString(@"Bully",@""),
    32                              NSLocalizedString(@"Aggressive",@""),nil]
    32                       NSLocalizedString(@"Aggressive",@""),nil];
    33     self.levelArray = array;
    33     self.levelArray = array;
    34     [array release];
    34     [array release];
    35 /*
    35 /*
    36     NSMutableArray *sprites = [[NSMutableArray alloc] initWithCapacity:[graveArray count];
    36     NSMutableArray *sprites = [[NSMutableArray alloc] initWithCapacity:[graveArray count];
    37     for (NSString *graveName in graveArray) {
    37     for (NSString *graveName in graveArray) {
    79 -(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    79 -(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    80     return [self.levelArray count];
    80     return [self.levelArray count];
    81 }
    81 }
    82 
    82 
    83 // Customize the appearance of table view cells.
    83 // Customize the appearance of table view cells.
    84 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    84 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    85     
       
    86     static NSString *CellIdentifier = @"Cell";
    85     static NSString *CellIdentifier = @"Cell";
    87     
    86     
       
    87     NSInteger row = [indexPath row];
    88     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    88     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    89     if (cell == nil) {
    89     if (cell == nil) {
    90         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    90         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    91     }
    91     }
    92     
    92     
    93     cell.textLabel.text = [[levelArray objectAtIndex:[indexPath row]] stringValue];
    93     cell.textLabel.text = [levelArray objectAtIndex:row];
    94     NSDictionary *hog = [[self.teamDictionary objectForKey:@"hedgehogs"] objectAtIndex:0]
    94     NSDictionary *hog = [[self.teamDictionary objectForKey:@"hedgehogs"] objectAtIndex:0];
    95     if ([cell.textLabel.text isEqualToString:[[hog objectForKey:@"level"]] stringValue]) {
    95     if ([[hog objectForKey:@"level"] intValue] == row) {
    96         cell.accessoryType = UITableViewCellAccessoryCheckmark;
    96         cell.accessoryType = UITableViewCellAccessoryCheckmark;
    97         self.lastIndexPath = indexPath;
    97         self.lastIndexPath = indexPath;
    98     } else {
    98     } else {
    99         cell.accessoryType = UITableViewCellAccessoryNone;
    99         cell.accessoryType = UITableViewCellAccessoryNone;
   100     }
   100     }
   101     
   101     
   102     cell.imageView.image = [levelSprites objectAtIndex:[indexPath row]]:
   102     cell.imageView.image = [levelSprites objectAtIndex:row];
   103     return cell;
   103     return cell;
   104 }
   104 }
   105 
   105 
   106 
   106 
   107 /*
   107 /*
   149 - (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
   149 - (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
   150     int newRow = [indexPath row];
   150     int newRow = [indexPath row];
   151     int oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1;
   151     int oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1;
   152     
   152     
   153     if (newRow != oldRow) {
   153     if (newRow != oldRow) {
   154 	NSMutableArray *hogs = [teamDictionary objectForKey:@"hedgehogs"];
   154         NSMutableArray *hogs = [teamDictionary objectForKey:@"hedgehogs"];
   155 
   155         
   156 	for (NSDictionary *hog in hogs) {
   156         for (NSMutableDictionary *hog in hogs) {
   157 		[hog setObject:[NSNumber numberWithInt:newRow] forKey:@"level"];
   157             [hog setObject:[NSNumber numberWithInt:newRow] forKey:@"level"];
   158 	}
   158         }
   159         
   159         
   160         // tell our boss to write this new stuff on disk
   160         // tell our boss to write this new stuff on disk
   161         [[NSNotificationCenter defaultCenter] postNotificationName:@"setWriteNeedTeams" object:nil];
   161         [[NSNotificationCenter defaultCenter] postNotificationName:@"setWriteNeedTeams" object:nil];
   162         [self.tableView reloadData];
   162         [self.tableView reloadData];
   163         
   163         
   164         self.lastIndexPath = indexPath;
   164         self.lastIndexPath = indexPath;
   165         [self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
   165         [self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
   166      
   166     }
   167     [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
   167     [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
   168     [self.navigationController popViewControllerAnimated:YES];
   168     [self.navigationController popViewControllerAnimated:YES];
   169 }
   169 }
   170 
   170 
   171 
   171 
   172 #pragma mark -
   172 #pragma mark -
   173 #pragma mark Memory management
   173 #pragma mark Memory management
   174 - (void)didReceiveMemoryWarning {
   174 -(void) didReceiveMemoryWarning {
   175     // Releases the view if it doesn't have a superview.
   175     // Releases the view if it doesn't have a superview.
   176     [super didReceiveMemoryWarning];
   176     [super didReceiveMemoryWarning];
   177     // Relinquish ownership any cached data, images, etc that aren't in use.
   177     // Relinquish ownership any cached data, images, etc that aren't in use.
   178 }
   178 }
   179 
   179 
   180 - (void)viewDidUnload {
   180 -(void) viewDidUnload {
   181     [super viewDidUnload];
   181     [super viewDidUnload];
   182     self.lastIndexPath = nil;
   182     self.lastIndexPath = nil;
   183     self.teamDictionary = nil;
   183     self.teamDictionary = nil;
   184     self.levelArray = nil;
   184     self.levelArray = nil;
   185     self.levelSprites = nil;
   185     self.levelSprites = nil;
   186 }
   186 }
   187 
   187 
   188 - (void)dealloc {
   188 -(void) dealloc {
   189     [levelArray release];
   189     [levelArray release];
   190     [levelSprites release];
   190     [levelSprites release];
   191     [teamDictionary release];
   191     [teamDictionary release];
   192     [lastIndexPath release];
   192     [lastIndexPath release];
   193     [super dealloc];
   193     [super dealloc];