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