cocoaTouch/FlagsViewController.m
changeset 3374 0d522416d97f
parent 3335 2520ee7a5484
child 3490 016b3172b645
equal deleted inserted replaced
3373:c1ff724a5c34 3374:0d522416d97f
     8 
     8 
     9 #import "FlagsViewController.h"
     9 #import "FlagsViewController.h"
    10 #import "CommodityFunctions.h"
    10 #import "CommodityFunctions.h"
    11 
    11 
    12 @implementation FlagsViewController
    12 @implementation FlagsViewController
    13 @synthesize teamDictionary, flagArray, flagSprites, lastIndexPath;
    13 @synthesize teamDictionary, flagArray, lastIndexPath;
    14 
    14 
    15 
    15 
    16 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation {
    16 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation {
    17 	return rotationManager(interfaceOrientation);
    17 	return rotationManager(interfaceOrientation);
    18 }
    18 }
    19 
    19 
    20 
    20 
    21 #pragma mark -
    21 #pragma mark -
    22 #pragma mark View lifecycle
    22 #pragma mark View lifecycle
    23 - (void)viewDidLoad {
    23 -(void) viewDidLoad {
    24     [super viewDidLoad];
    24     [super viewDidLoad];
    25 
    25 
    26     NSString *flagsDirectory = FLAGS_DIRECTORY();
    26     self.flagArray = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:FLAGS_DIRECTORY() error:NULL];
    27     NSArray *array = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:flagsDirectory error:NULL];
       
    28     self.flagArray = array;
       
    29     
       
    30     NSMutableArray *spriteArray = [[NSMutableArray alloc] initWithCapacity:[flagArray count]];
       
    31     for (NSString *flagName in flagArray) {
       
    32         NSString *flagFile = [[NSString alloc] initWithFormat:@"%@/%@", flagsDirectory, flagName];
       
    33         UIImage *flagSprite = [[UIImage alloc] initWithContentsOfFile:flagFile];
       
    34         [flagFile release];
       
    35         [spriteArray addObject:flagSprite];
       
    36         [flagSprite release];
       
    37     }
       
    38     self.flagSprites = spriteArray;
       
    39     [spriteArray release];
       
    40 }
    27 }
    41 
    28 
    42 - (void)viewWillAppear:(BOOL)animated {
    29 -(void) viewWillAppear:(BOOL)animated {
    43     [super viewWillAppear:animated];
    30     [super viewWillAppear:animated];
    44     [self.tableView reloadData];
    31     [self.tableView reloadData];
    45     [self.tableView setContentOffset:CGPointMake(0,0) animated:NO];
    32     [self.tableView setContentOffset:CGPointMake(0,0) animated:NO];
    46 }
    33 }
    47 
    34 
    48 /*
       
    49 - (void)viewDidAppear:(BOOL)animated {
       
    50     [super viewDidAppear:animated];
       
    51 }
       
    52 */
       
    53 /*
       
    54 - (void)viewWillDisappear:(BOOL)animated {
       
    55     [super viewWillDisappear:animated];
       
    56 }
       
    57 */
       
    58 /*
       
    59 - (void)viewDidDisappear:(BOOL)animated {
       
    60     [super viewDidDisappear:animated];
       
    61 }
       
    62 */
       
    63 
       
    64 
    35 
    65 #pragma mark -
    36 #pragma mark -
    66 #pragma mark Table view data source
    37 #pragma mark Table view data source
    67 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    38 -(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
    68     return 1;
    39     return 1;
    69 }
    40 }
    70 
    41 
    71 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    42 -(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    72     return [flagArray count];
    43     return [flagArray count];
    73 }
    44 }
    74 
    45 
    75 // Customize the appearance of table view cells.
    46 // Customize the appearance of table view cells.
    76 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    47 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    77     
    48     
    78     static NSString *CellIdentifier = @"Cell";
    49     static NSString *CellIdentifier = @"Cell";
    79     
    50     
    80     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    51     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    81     if (cell == nil) {
    52     if (cell == nil) {
    82         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    53         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    83     }
    54     }
    84     
    55     
    85     cell.imageView.image = [flagSprites objectAtIndex:[indexPath row]];
    56     NSString *flag = [flagArray objectAtIndex:[indexPath row]];
    86     cell.textLabel.text = [[flagArray objectAtIndex:[indexPath row]] stringByDeletingPathExtension];
    57     
       
    58     NSString *flagFile = [[NSString alloc] initWithFormat:@"%@/%@", FLAGS_DIRECTORY(), flag];
       
    59     UIImage *flagSprite = [[UIImage alloc] initWithContentsOfFile:flagFile];
       
    60     [flagFile release];
       
    61     cell.imageView.image = flagSprite;
       
    62     [flagSprite release];
       
    63     
       
    64     cell.textLabel.text = [flag stringByDeletingPathExtension];
    87     if ([cell.textLabel.text isEqualToString:[self.teamDictionary objectForKey:@"flag"]]) {
    65     if ([cell.textLabel.text isEqualToString:[self.teamDictionary objectForKey:@"flag"]]) {
    88         cell.accessoryType = UITableViewCellAccessoryCheckmark;
    66         cell.accessoryType = UITableViewCellAccessoryCheckmark;
    89         self.lastIndexPath = indexPath;
    67         self.lastIndexPath = indexPath;
    90     } else {
    68     } else {
    91         cell.accessoryType = UITableViewCellAccessoryNone;
    69         cell.accessoryType = UITableViewCellAccessoryNone;
    93     
    71     
    94     return cell;
    72     return cell;
    95 }
    73 }
    96 
    74 
    97 
    75 
    98 /*
       
    99 // Override to support conditional editing of the table view.
       
   100 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
       
   101     // Return NO if you do not want the specified item to be editable.
       
   102     return YES;
       
   103 }
       
   104 */
       
   105 
       
   106 
       
   107 /*
       
   108 // Override to support editing the table view.
       
   109 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
       
   110     
       
   111     if (editingStyle == UITableViewCellEditingStyleDelete) {
       
   112         // Delete the row from the data source
       
   113         [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
       
   114     }   
       
   115     else if (editingStyle == UITableViewCellEditingStyleInsert) {
       
   116         // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
       
   117     }   
       
   118 }
       
   119 */
       
   120 
       
   121 
       
   122 /*
       
   123 // Override to support rearranging the table view.
       
   124 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
       
   125 }
       
   126 */
       
   127 
       
   128 
       
   129 /*
       
   130 // Override to support conditional rearranging of the table view.
       
   131 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
       
   132     // Return NO if you do not want the item to be re-orderable.
       
   133     return YES;
       
   134 }
       
   135 */
       
   136 
       
   137 
       
   138 #pragma mark -
    76 #pragma mark -
   139 #pragma mark Table view delegate
    77 #pragma mark Table view delegate
   140 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    78 -(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
   141     int newRow = [indexPath row];
    79     int newRow = [indexPath row];
   142     int oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1;
    80     int oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1;
   143     
    81     
   144     if (newRow != oldRow) {
    82     if (newRow != oldRow) {
   145         // if the two selected rows differ update data on the hog dictionary and reload table content
    83         // if the two selected rows differ update data on the hog dictionary and reload table content
   146         [self.teamDictionary setValue:[[flagArray objectAtIndex:newRow] stringByDeletingPathExtension] forKey:@"flag"];
    84         [self.teamDictionary setValue:[[flagArray objectAtIndex:newRow] stringByDeletingPathExtension] forKey:@"flag"];
   147 
    85 
   148         // tell our boss to write this new stuff on disk
    86         // tell our boss to write this new stuff on disk
   149         [[NSNotificationCenter defaultCenter] postNotificationName:@"setWriteNeedTeams" object:nil];
    87         [[NSNotificationCenter defaultCenter] postNotificationName:@"setWriteNeedTeams" object:nil];
   150         [self.tableView reloadData];
       
   151 
    88 
       
    89         UITableViewCell *newCell = [aTableView cellForRowAtIndexPath:indexPath];
       
    90         newCell.accessoryType = UITableViewCellAccessoryCheckmark;
       
    91         UITableViewCell *oldCell = [aTableView cellForRowAtIndexPath:lastIndexPath];
       
    92         oldCell.accessoryType = UITableViewCellAccessoryNone;
   152         self.lastIndexPath = indexPath;
    93         self.lastIndexPath = indexPath;
   153         [self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
    94         [aTableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
   154     }
    95     }
   155     [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
    96     [aTableView deselectRowAtIndexPath:indexPath animated:YES];
   156     [self.navigationController popViewControllerAnimated:YES];
    97     [self.navigationController popViewControllerAnimated:YES];
   157 }
    98 }
   158 
    99 
   159 
   100 
   160 #pragma mark -
   101 #pragma mark -
   161 #pragma mark Memory management
   102 #pragma mark Memory management
   162 - (void)didReceiveMemoryWarning {
   103 -(void) didReceiveMemoryWarning {
   163     // Releases the view if it doesn't have a superview.
   104     // Releases the view if it doesn't have a superview.
   164     [super didReceiveMemoryWarning];
   105     [super didReceiveMemoryWarning];
   165     // Relinquish ownership any cached data, images, etc that aren't in use.
   106     // Relinquish ownership any cached data, images, etc that aren't in use.
   166 }
   107 }
   167 
   108 
   168 - (void)viewDidUnload {
   109 -(void) viewDidUnload {
   169     self.teamDictionary = nil;
   110     self.teamDictionary = nil;
   170     self.lastIndexPath = nil;
   111     self.lastIndexPath = nil;
   171     self.flagArray = nil;
   112     self.flagArray = nil;
   172     self.flagSprites = nil;
       
   173     [super viewDidUnload];
   113     [super viewDidUnload];
   174 }
   114 }
   175 
   115 
   176 
   116 -(void) dealloc {
   177 - (void)dealloc {
       
   178     [teamDictionary release];
   117     [teamDictionary release];
   179     [lastIndexPath release];
   118     [lastIndexPath release];
   180     [flagArray release];
   119     [flagArray release];
   181     [flagSprites release];
       
   182     [super dealloc];
   120     [super dealloc];
   183 }
   121 }
   184 
   122 
   185 
   123 
   186 @end
   124 @end