cocoaTouch/GravesViewController.m
changeset 3516 a8c673657b79
parent 3510 23145a950eae
parent 3515 3e8635f43972
child 3529 0e968ba12a84
equal deleted inserted replaced
3510:23145a950eae 3516:a8c673657b79
     1 //
       
     2 //  GravesViewController.m
       
     3 //  HedgewarsMobile
       
     4 //
       
     5 //  Created by Vittorio on 02/04/10.
       
     6 //  Copyright 2010 __MyCompanyName__. All rights reserved.
       
     7 //
       
     8 
       
     9 #import "GravesViewController.h"
       
    10 #import "CommodityFunctions.h"
       
    11 #import "UIImageExtra.h"
       
    12 
       
    13 @implementation GravesViewController
       
    14 @synthesize teamDictionary, graveArray, 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 grave names and store them into graveArray
       
    28     self.graveArray = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:GRAVES_DIRECTORY() error:NULL];
       
    29 }
       
    30 
       
    31 -(void) viewWillAppear:(BOOL)animated {
       
    32     [super viewWillAppear:animated];
       
    33     [self.tableView reloadData];
       
    34     // this moves the tableview to the top
       
    35     [self.tableView setContentOffset:CGPointMake(0,0) animated:NO];
       
    36 }
       
    37 
       
    38 
       
    39 #pragma mark -
       
    40 #pragma mark Table view data source
       
    41 -(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
       
    42     return 1;
       
    43 }
       
    44 
       
    45 -(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
       
    46     return [self.graveArray count];
       
    47 }
       
    48 
       
    49 // Customize the appearance of table view cells.
       
    50 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
       
    51     
       
    52     static NSString *CellIdentifier = @"Cell";
       
    53     
       
    54     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
       
    55     if (cell == nil)
       
    56         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
       
    57     
       
    58     NSString *grave = [graveArray objectAtIndex:[indexPath row]];
       
    59     cell.textLabel.text = [grave stringByDeletingPathExtension];
       
    60     
       
    61     if ([grave isEqualToString:[teamDictionary objectForKey:@"grave"]]) {
       
    62         cell.accessoryType = UITableViewCellAccessoryCheckmark;
       
    63         self.lastIndexPath = indexPath;
       
    64     } else {
       
    65         cell.accessoryType = UITableViewCellAccessoryNone;
       
    66     }
       
    67 
       
    68     NSString *graveFilePath = [[NSString alloc] initWithFormat:@"%@/%@",GRAVES_DIRECTORY(),grave];
       
    69     // because we also have multi frame graves, let's take the first one only
       
    70     UIImage *graveSprite = [[UIImage alloc] initWithContentsOfFile:graveFilePath andCutAt:CGRectMake(0, 0, 32, 32)];
       
    71     [graveFilePath release];        
       
    72     cell.imageView.image = graveSprite;
       
    73     [graveSprite release];
       
    74     
       
    75     return cell;
       
    76 }
       
    77 
       
    78 
       
    79 #pragma mark -
       
    80 #pragma mark Table view delegate
       
    81 -(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
       
    82     int newRow = [indexPath row];
       
    83     int oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1;
       
    84     
       
    85     if (newRow != oldRow) {
       
    86         [teamDictionary setObject:[[graveArray objectAtIndex:newRow] stringByDeletingPathExtension] forKey:@"grave"];
       
    87         
       
    88         // tell our boss to write this new stuff on disk
       
    89         [[NSNotificationCenter defaultCenter] postNotificationName:@"setWriteNeedTeams" object:nil];
       
    90         
       
    91         UITableViewCell *newCell = [aTableView cellForRowAtIndexPath:indexPath];
       
    92         newCell.accessoryType = UITableViewCellAccessoryCheckmark;
       
    93         UITableViewCell *oldCell = [aTableView cellForRowAtIndexPath:lastIndexPath];
       
    94         oldCell.accessoryType = UITableViewCellAccessoryNone;
       
    95         self.lastIndexPath = indexPath;
       
    96         [aTableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
       
    97     }
       
    98     [aTableView deselectRowAtIndexPath:indexPath animated:YES];
       
    99     [self.navigationController popViewControllerAnimated:YES];
       
   100 }
       
   101 
       
   102 
       
   103 #pragma mark -
       
   104 #pragma mark Memory management
       
   105 - (void)didReceiveMemoryWarning {
       
   106     // Releases the view if it doesn't have a superview.
       
   107     [super didReceiveMemoryWarning];
       
   108     // Relinquish ownership any cached data, images, etc that aren't in use.
       
   109 }
       
   110 
       
   111 - (void)viewDidUnload {
       
   112     self.lastIndexPath = nil;
       
   113     self.teamDictionary = nil;
       
   114     self.graveArray = nil;
       
   115     [super viewDidUnload];
       
   116     MSG_DIDUNLOAD();
       
   117 }
       
   118 
       
   119 - (void)dealloc {
       
   120     [graveArray release];
       
   121     [teamDictionary release];
       
   122     [lastIndexPath release];
       
   123     [super dealloc];
       
   124 }
       
   125 
       
   126 
       
   127 @end
       
   128