cocoaTouch/HogHatViewController.m
changeset 3315 4e2813713358
parent 3312 6d8f1c76756d
child 3316 52f0482b043f
equal deleted inserted replaced
3314:bab4a078263f 3315:4e2813713358
     8 
     8 
     9 #import "HogHatViewController.h"
     9 #import "HogHatViewController.h"
    10 
    10 
    11 
    11 
    12 @implementation HogHatViewController
    12 @implementation HogHatViewController
    13 @synthesize hatList, hatSprites, hog;
    13 @synthesize teamDictionary, hatArray, hatSprites, lastIndexPath, selectedHog;
    14 
    14 
    15 #pragma mark -
    15 #pragma mark -
    16 #pragma mark View lifecycle
    16 #pragma mark View lifecycle
    17 
       
    18 
       
    19 - (void)viewDidLoad {
    17 - (void)viewDidLoad {
    20     [super viewDidLoad];
    18     [super viewDidLoad];
    21 
    19 
    22     //NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    20     // load all the hat file names and store them into hatArray
    23     NSString *hatPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingString:@"/Data/Graphics/Hats/"];
    21     NSString *hatPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingString:@"/Data/Graphics/Hats/"];
    24     NSArray *array = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:hatPath
    22     NSArray *array = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:hatPath error:NULL];
    25                                                                          error:NULL];
    23     self.hatArray = array;
    26     self.hatList = array;
    24     
    27     NSMutableArray *spriteArray = [[NSMutableArray alloc] initWithCapacity:[hatList count]];
    25     // load all the hat images from the previous array but save only the first sprite and store it in hatSprites
    28     for (int i=0; i< [hatList count]; i++) {
    26     NSMutableArray *spriteArray = [[NSMutableArray alloc] initWithCapacity:[hatArray count]];
    29         NSString *hatFile = [hatPath stringByAppendingString:[hatList objectAtIndex:i]];
    27     for (int i=0; i< [hatArray count]; i++) {
       
    28         NSString *hatFile = [hatPath stringByAppendingString:[hatArray objectAtIndex:i]];
    30         
    29         
    31         UIImage *image = [[UIImage alloc] initWithContentsOfFile: hatFile];
    30         UIImage *image = [[UIImage alloc] initWithContentsOfFile: hatFile];
    32         CGRect firstSpriteArea = CGRectMake(0, 0, 32, 32);
    31         CGRect firstSpriteArea = CGRectMake(0, 0, 32, 32);
    33         CGImageRef cgImgage = CGImageCreateWithImageInRect([image CGImage], firstSpriteArea);
    32         CGImageRef cgImgage = CGImageCreateWithImageInRect([image CGImage], firstSpriteArea);
    34         [image release];
    33         [image release];
    38         CGImageRelease(cgImgage);
    37         CGImageRelease(cgImgage);
    39         [hatSprite release];
    38         [hatSprite release];
    40     }
    39     }
    41     self.hatSprites = spriteArray;
    40     self.hatSprites = spriteArray;
    42     [spriteArray release];
    41     [spriteArray release];
    43     //NSLog(@"%@", hatList);
       
    44 }
    42 }
    45 
    43 
    46 - (void)viewWillAppear:(BOOL)animated {
    44 - (void)viewWillAppear:(BOOL)animated {
    47     [super viewWillAppear:animated];
    45     [super viewWillAppear:animated];
    48     self.title = [hog objectForKey:@"hogname"];
    46     self.title = [[[teamDictionary objectForKey:@"hedgehogs"] objectAtIndex:selectedHog] objectForKey:@"hogname"];
       
    47 
    49     // this updates the hog name and its hat
    48     // this updates the hog name and its hat
    50     [self.tableView reloadData];
    49     [self.tableView reloadData];
    51     // this moves the tableview to the top
    50     // this moves the tableview to the top
    52     [self.tableView setContentOffset:CGPointMake(0,0) animated:NO];
    51     [self.tableView setContentOffset:CGPointMake(0,0) animated:NO];
    53 }
    52 }
    67     [super viewDidDisappear:animated];
    66     [super viewDidDisappear:animated];
    68 }
    67 }
    69 */
    68 */
    70 
    69 
    71 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    70 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    72     // Override to allow orientations other than the default portrait orientation.
    71     return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
    73     return YES;
       
    74 }
    72 }
    75 
    73 
    76 
    74 
    77 #pragma mark -
    75 #pragma mark -
    78 #pragma mark Table view data source
    76 #pragma mark Table view data source
    83 -(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    81 -(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    84     NSInteger rows;
    82     NSInteger rows;
    85     if (0 == section) 
    83     if (0 == section) 
    86         rows = 1;
    84         rows = 1;
    87     else
    85     else
    88         rows = [self.hatList count];
    86         rows = [self.hatArray count];
    89     return rows;
    87     return rows;
    90 }
    88 }
    91 
    89 
    92 // Customize the appearance of table view cells.
    90 // Customize the appearance of table view cells.
    93 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    91 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    97     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    95     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    98     if (cell == nil) {
    96     if (cell == nil) {
    99         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    97         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
   100     }
    98     }
   101     
    99     
       
   100     NSDictionary *hog = [[teamDictionary objectForKey:@"hedgehogs"] objectAtIndex:selectedHog];
   102     if (0 == [indexPath section]) {
   101     if (0 == [indexPath section]) {
   103         cell.textLabel.text = [hog objectForKey:@"hogname"];
   102         cell.textLabel.text = self.title;
   104         cell.imageView.image = nil;
   103         cell.imageView.image = nil;
   105         cell.accessoryType = UITableViewCellAccessoryNone;
   104         cell.accessoryType = UITableViewCellAccessoryNone;
   106     } else {
   105     } else {
   107         cell.textLabel.text = [[hatList objectAtIndex:[indexPath row]] stringByDeletingPathExtension];
   106         cell.textLabel.text = [[hatArray objectAtIndex:[indexPath row]] stringByDeletingPathExtension];
   108         if ([cell.textLabel.text isEqualToString:[hog objectForKey:@"hat"]]) {
   107         if ([cell.textLabel.text isEqualToString:[hog objectForKey:@"hat"]]) {
   109             cell.accessoryType = UITableViewCellAccessoryCheckmark;
   108             cell.accessoryType = UITableViewCellAccessoryCheckmark;
       
   109             self.lastIndexPath = indexPath;
   110         } else {
   110         } else {
   111             cell.accessoryType = UITableViewCellAccessoryNone;
   111             cell.accessoryType = UITableViewCellAccessoryNone;
   112         }
   112         }
   113         
   113         
   114         cell.imageView.image = [hatSprites objectAtIndex:[indexPath row]];
   114         cell.imageView.image = [hatSprites objectAtIndex:[indexPath row]];
   159 
   159 
   160 
   160 
   161 #pragma mark -
   161 #pragma mark -
   162 #pragma mark Table view delegate
   162 #pragma mark Table view delegate
   163 
   163 
   164 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
   164 - (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
   165     // Navigation logic may go here. Create and push another view controller.
   165     if (1 == [indexPath section]) {
   166 	/*
   166         int newRow = [indexPath row];
   167 	 <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
   167         int oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1;
   168      // ...
   168         
   169      // Pass the selected object to the new view controller.
   169         if (newRow != oldRow) {
   170 	 [self.navigationController pushViewController:detailViewController animated:YES];
   170             // if the two selected rows differ update data on the hog dictionary and reload table content
   171 	 [detailViewController release];
   171             NSDictionary *oldHog = [[teamDictionary objectForKey:@"hedgehogs"] objectAtIndex:selectedHog];
   172 	 */
   172 
       
   173             NSMutableDictionary *newHog = [[NSMutableDictionary alloc] initWithDictionary: oldHog];
       
   174             [newHog setObject:[[hatArray objectAtIndex:newRow] stringByDeletingPathExtension] forKey:@"hat"];
       
   175             [[teamDictionary objectForKey:@"hedgehogs"] replaceObjectAtIndex:selectedHog withObject:newHog];
       
   176             [newHog release];
       
   177             
       
   178             // tell our boss to write this new stuff on disk
       
   179             [[NSNotificationCenter defaultCenter] postNotificationName:@"setWriteNeedTeams" object:nil];
       
   180             [self.tableView reloadData];
       
   181 
       
   182             self.lastIndexPath = indexPath;
       
   183             [self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
       
   184         } 
       
   185         [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
       
   186     }
   173 }
   187 }
   174 
   188 
   175 
   189 
   176 #pragma mark -
   190 #pragma mark -
   177 #pragma mark Memory management
   191 #pragma mark Memory management
   178 
       
   179 - (void)didReceiveMemoryWarning {
   192 - (void)didReceiveMemoryWarning {
   180     // Releases the view if it doesn't have a superview.
   193     // Releases the view if it doesn't have a superview.
   181     [super didReceiveMemoryWarning];
   194     [super didReceiveMemoryWarning];
   182     
       
   183     // Relinquish ownership any cached data, images, etc that aren't in use.
   195     // Relinquish ownership any cached data, images, etc that aren't in use.
   184 }
   196 }
   185 
   197 
   186 - (void)viewDidUnload {
   198 - (void)viewDidUnload {
   187     [super viewDidUnload];
   199     [super viewDidUnload];
   188     self.hatList = nil;
   200     self.lastIndexPath = nil;
   189     self.hog = nil;
   201     self.hatSprites = nil;
   190     // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
   202     self.teamDictionary = nil;
   191     // For example: self.myOutlet = nil;
   203     self.hatArray = nil;
   192 }
   204 }
   193 
       
   194 
   205 
   195 - (void)dealloc {
   206 - (void)dealloc {
   196     [hog release];
   207     [hatArray release];
   197     [hatList release];
   208     [teamDictionary release];
       
   209     [hatSprites release];
       
   210     [lastIndexPath release];
   198     [super dealloc];
   211     [super dealloc];
   199 }
   212 }
   200 
   213 
   201 
   214 
   202 @end
   215 @end