cocoaTouch/FlagsViewController.m
changeset 3516 a8c673657b79
parent 3510 23145a950eae
parent 3515 3e8635f43972
child 3529 0e968ba12a84
equal deleted inserted replaced
3510:23145a950eae 3516:a8c673657b79
     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, lastIndexPath;
       
    14 
       
    15 
       
    16 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation {
       
    17 	return rotationManager(interfaceOrientation);
       
    18 }
       
    19 
       
    20 
       
    21 #pragma mark -
       
    22 #pragma mark View lifecycle
       
    23 -(void) viewDidLoad {
       
    24     [super viewDidLoad];
       
    25 
       
    26     self.flagArray = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:FLAGS_DIRECTORY() error:NULL];
       
    27 }
       
    28 
       
    29 -(void) viewWillAppear:(BOOL)animated {
       
    30     [super viewWillAppear:animated];
       
    31     [self.tableView reloadData];
       
    32     [self.tableView setContentOffset:CGPointMake(0,0) animated:NO];
       
    33 }
       
    34 
       
    35 
       
    36 #pragma mark -
       
    37 #pragma mark Table view data source
       
    38 -(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
       
    39     return 1;
       
    40 }
       
    41 
       
    42 -(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
       
    43     return [flagArray count];
       
    44 }
       
    45 
       
    46 // Customize the appearance of table view cells.
       
    47 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
       
    48     
       
    49     static NSString *CellIdentifier = @"Cell";
       
    50     
       
    51     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
       
    52     if (cell == nil) {
       
    53         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
       
    54     }
       
    55     
       
    56     NSString *flag = [flagArray objectAtIndex:[indexPath row]];
       
    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];
       
    65     if ([cell.textLabel.text isEqualToString:[self.teamDictionary objectForKey:@"flag"]]) {
       
    66         cell.accessoryType = UITableViewCellAccessoryCheckmark;
       
    67         self.lastIndexPath = indexPath;
       
    68     } else {
       
    69         cell.accessoryType = UITableViewCellAccessoryNone;
       
    70     }
       
    71     
       
    72     return cell;
       
    73 }
       
    74 
       
    75 
       
    76 #pragma mark -
       
    77 #pragma mark Table view delegate
       
    78 -(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
       
    79     int newRow = [indexPath row];
       
    80     int oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1;
       
    81     
       
    82     if (newRow != oldRow) {
       
    83         // if the two selected rows differ update data on the hog dictionary and reload table content
       
    84         [self.teamDictionary setValue:[[flagArray objectAtIndex:newRow] stringByDeletingPathExtension] forKey:@"flag"];
       
    85 
       
    86         // tell our boss to write this new stuff on disk
       
    87         [[NSNotificationCenter defaultCenter] postNotificationName:@"setWriteNeedTeams" object:nil];
       
    88 
       
    89         UITableViewCell *newCell = [aTableView cellForRowAtIndexPath:indexPath];
       
    90         newCell.accessoryType = UITableViewCellAccessoryCheckmark;
       
    91         UITableViewCell *oldCell = [aTableView cellForRowAtIndexPath:lastIndexPath];
       
    92         oldCell.accessoryType = UITableViewCellAccessoryNone;
       
    93         self.lastIndexPath = indexPath;
       
    94         [aTableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
       
    95     }
       
    96     [aTableView deselectRowAtIndexPath:indexPath animated:YES];
       
    97     [self.navigationController popViewControllerAnimated:YES];
       
    98 }
       
    99 
       
   100 
       
   101 #pragma mark -
       
   102 #pragma mark Memory management
       
   103 -(void) didReceiveMemoryWarning {
       
   104     // Releases the view if it doesn't have a superview.
       
   105     [super didReceiveMemoryWarning];
       
   106     // Relinquish ownership any cached data, images, etc that aren't in use.
       
   107 }
       
   108 
       
   109 -(void) viewDidUnload {
       
   110     self.teamDictionary = nil;
       
   111     self.lastIndexPath = nil;
       
   112     self.flagArray = nil;
       
   113     [super viewDidUnload];
       
   114     MSG_DIDUNLOAD();
       
   115 }
       
   116 
       
   117 -(void) dealloc {
       
   118     [teamDictionary release];
       
   119     [lastIndexPath release];
       
   120     [flagArray release];
       
   121     [super dealloc];
       
   122 }
       
   123 
       
   124 
       
   125 @end
       
   126