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