project_files/HedgewarsMobile/Classes/FortsViewController.m
branchios-develop
changeset 12872 00215a7ec5f5
parent 11184 e03bb95edf44
equal deleted inserted replaced
12871:2c06b1120749 12872:00215a7ec5f5
    24 
    24 
    25 @implementation FortsViewController
    25 @implementation FortsViewController
    26 @synthesize teamDictionary, fortArray, lastIndexPath;
    26 @synthesize teamDictionary, fortArray, lastIndexPath;
    27 
    27 
    28 
    28 
    29 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation {
    29 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    30     return rotationManager(interfaceOrientation);
    30     return rotationManager(interfaceOrientation);
    31 }
    31 }
    32 
    32 
    33 
    33 
    34 #pragma mark -
    34 #pragma mark -
    35 #pragma mark View lifecycle
    35 #pragma mark View lifecycle
    36 -(void) viewDidLoad {
    36 - (void)viewDidLoad {
    37     [super viewDidLoad];
    37     [super viewDidLoad];
    38 
    38 
    39     NSArray *directoryContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:FORTS_DIRECTORY() error:NULL];
    39     NSArray *directoryContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:FORTS_DIRECTORY() error:NULL];
    40     NSMutableArray *filteredContents = [[NSMutableArray alloc] initWithCapacity:([directoryContents count] / IMGNUM_PER_FORT)];
    40     NSMutableArray *filteredContents = [[NSMutableArray alloc] initWithCapacity:([directoryContents count] / IMGNUM_PER_FORT)];
    41     // we assume here that fort's images has one image with the 'L.png' suffix and we remove this suffix to get the correct name
    41     // we assume here that fort's images has one image with the 'L.png' suffix and we remove this suffix to get the correct name
    47             NSString *correctName = [currentName stringByReplacingOccurrencesOfString:@"L.png" withString:@""];
    47             NSString *correctName = [currentName stringByReplacingOccurrencesOfString:@"L.png" withString:@""];
    48             [filteredContents addObject:correctName];
    48             [filteredContents addObject:correctName];
    49         }
    49         }
    50     }
    50     }
    51     self.fortArray = filteredContents;
    51     self.fortArray = filteredContents;
    52     [filteredContents release];
       
    53 
    52 
    54     // statically set row height instead of using delegate method for performance reasons
    53     // statically set row height instead of using delegate method for performance reasons
    55     self.tableView.rowHeight = 128;
    54     self.tableView.rowHeight = 128;
    56 
    55 
    57     self.title = NSLocalizedString(@"Choose team fort",@"");
    56     self.title = NSLocalizedString(@"Choose team fort",@"");
    58 }
    57 }
    59 
    58 
    60 
    59 
    61 -(void) viewWillAppear:(BOOL)animated {
    60 - (void)viewWillAppear:(BOOL)animated {
    62     [super viewWillAppear:animated];
    61     [super viewWillAppear:animated];
    63     [self.tableView reloadData];
    62     [self.tableView reloadData];
    64     [self.tableView setContentOffset:CGPointMake(0,0) animated:NO];
    63     [self.tableView setContentOffset:CGPointMake(0,0) animated:NO];
    65 }
    64 }
    66 
    65 
    67 
    66 
    68 #pragma mark -
    67 #pragma mark -
    69 #pragma mark Table view data source
    68 #pragma mark Table view data source
    70 -(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
    69 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    71     return 1;
    70     return 1;
    72 }
    71 }
    73 
    72 
    74 -(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    73 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    75     return [self.fortArray count];
    74     return [self.fortArray count];
    76 }
    75 }
    77 
    76 
    78 // Customize the appearance of table view cells.
    77 // Customize the appearance of table view cells.
    79 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    78 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    80     static NSString *CellIdentifier = @"Cell";
    79     static NSString *CellIdentifier = @"Cell";
    81 
    80 
    82     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    81     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    83     if (cell == nil)
    82     if (cell == nil)
    84         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
    83         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
    85                                        reuseIdentifier:CellIdentifier] autorelease];
    84                                        reuseIdentifier:CellIdentifier];
    86 
    85 
    87     NSString *fortName = [fortArray objectAtIndex:[indexPath row]];
    86     NSString *fortName = [fortArray objectAtIndex:[indexPath row]];
    88     cell.textLabel.text = fortName;
    87     cell.textLabel.text = fortName;
    89 
    88 
    90     NSString *fortFile = [[NSString alloc] initWithFormat:@"%@/%@-preview.png", FORTS_DIRECTORY(), fortName];
    89     NSString *fortFile = [[NSString alloc] initWithFormat:@"%@/%@-preview.png", FORTS_DIRECTORY(), fortName];
    91     UIImage *fortSprite = [[UIImage alloc] initWithContentsOfFile:fortFile];
    90     UIImage *fortSprite = [[UIImage alloc] initWithContentsOfFile:fortFile];
    92     [fortFile release];
       
    93     cell.imageView.image = fortSprite;
    91     cell.imageView.image = fortSprite;
    94     [fortSprite release];
       
    95 
    92 
    96     //cell.detailTextLabel.text = @"Insert funny description here";
    93     //cell.detailTextLabel.text = @"Insert funny description here";
    97     if ([cell.textLabel.text isEqualToString:[self.teamDictionary objectForKey:@"fort"]]) {
    94     if ([cell.textLabel.text isEqualToString:[self.teamDictionary objectForKey:@"fort"]]) {
    98         cell.accessoryType = UITableViewCellAccessoryCheckmark;
    95         cell.accessoryType = UITableViewCellAccessoryCheckmark;
    99         self.lastIndexPath = indexPath;
    96         self.lastIndexPath = indexPath;
   105 }
   102 }
   106 
   103 
   107 
   104 
   108 #pragma mark -
   105 #pragma mark -
   109 #pragma mark Table view delegate
   106 #pragma mark Table view delegate
   110 -(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
   107 - (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
   111     NSInteger newRow = [indexPath row];
   108     NSInteger newRow = [indexPath row];
   112     NSInteger oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1;
   109     NSInteger oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1;
   113 
   110 
   114     if (newRow != oldRow) {
   111     if (newRow != oldRow) {
   115         // if the two selected rows differ update data on the hog dictionary and reload table content
   112         // if the two selected rows differ update data on the hog dictionary and reload table content
   130 }
   127 }
   131 
   128 
   132 
   129 
   133 #pragma mark -
   130 #pragma mark -
   134 #pragma mark Memory management
   131 #pragma mark Memory management
   135 -(void) didReceiveMemoryWarning {
   132 
       
   133 - (void)didReceiveMemoryWarning {
   136     self.lastIndexPath = nil;
   134     self.lastIndexPath = nil;
   137     MSG_MEMCLEAN();
   135     MSG_MEMCLEAN();
   138     [super didReceiveMemoryWarning];
   136     [super didReceiveMemoryWarning];
   139 }
   137 }
   140 
   138 
   141 -(void) viewDidUnload {
       
   142     self.teamDictionary = nil;
       
   143     self.lastIndexPath = nil;
       
   144     self.fortArray = nil;
       
   145     MSG_DIDUNLOAD();
       
   146     [super viewDidUnload];
       
   147 }
       
   148 
       
   149 
       
   150 -(void) dealloc {
       
   151     releaseAndNil(teamDictionary);
       
   152     releaseAndNil(lastIndexPath);
       
   153     releaseAndNil(fortArray);
       
   154     [super dealloc];
       
   155 }
       
   156 
       
   157 
       
   158 @end
   139 @end
   159 
   140