cocoaTouch/SingleTeamViewController.m
changeset 3315 4e2813713358
parent 3312 6d8f1c76756d
child 3316 52f0482b043f
equal deleted inserted replaced
3314:bab4a078263f 3315:4e2813713358
     8 
     8 
     9 #import "SingleTeamViewController.h"
     9 #import "SingleTeamViewController.h"
    10 #import "HogHatViewController.h"
    10 #import "HogHatViewController.h"
    11 
    11 
    12 @implementation SingleTeamViewController
    12 @implementation SingleTeamViewController
    13 @synthesize hogsList, hatList, secondaryItems, teamName;
    13 @synthesize teamDictionary, hatArray, secondaryItems;
    14 
    14 
    15 
    15 
    16 #pragma mark -
    16 #pragma mark -
    17 #pragma mark View lifecycle
    17 #pragma mark View lifecycle
    18 - (void)viewDidLoad {
    18 - (void)viewDidLoad {
    31                              NSLocalizedString(@"Fort",@""),
    31                              NSLocalizedString(@"Fort",@""),
    32                              NSLocalizedString(@"Flag",@""),
    32                              NSLocalizedString(@"Flag",@""),
    33                              NSLocalizedString(@"Level",@""),nil];
    33                              NSLocalizedString(@"Level",@""),nil];
    34     self.secondaryItems = array;
    34     self.secondaryItems = array;
    35     [array release];
    35     [array release];
    36 }
    36     
    37 
    37     // load data about the team and extract info
       
    38     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
       
    39     NSString *teamFile = [[NSString alloc] initWithFormat:@"%@/Teams/%@.plist",[paths objectAtIndex:0],self.title];
       
    40     NSMutableDictionary *teamDict = [[NSMutableDictionary alloc] initWithContentsOfFile:teamFile];
       
    41     [teamFile release];
       
    42     self.teamDictionary = teamDict;
       
    43     [teamDict release];
       
    44 
       
    45     // listen if any childController modifies the plist and write it if needed
       
    46     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(setWriteNeeded) name:@"setWriteNeedTeams" object:nil];
       
    47     isWriteNeeded = NO;
       
    48 }
    38 
    49 
    39 - (void)viewWillAppear:(BOOL)animated {
    50 - (void)viewWillAppear:(BOOL)animated {
    40     [super viewWillAppear:animated];
    51     [super viewWillAppear:animated];
    41     
    52 
    42     // load data about the team and extract info
       
    43     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
       
    44     NSString *teamFile = [[NSString alloc] initWithFormat:@"%@/Teams/%@.plist",[paths objectAtIndex:0],self.teamName];
       
    45     NSDictionary *teamDict = [[NSDictionary alloc] initWithContentsOfFile:teamFile];
       
    46     [teamFile release];
       
    47     
       
    48     // grab the hog list
       
    49     self.hogsList = [teamDict objectForKey:@"hedgehogs"];
       
    50     // grab the name of the team
    53     // grab the name of the team
    51     self.teamName = [teamDict objectForKey:@"teamname"];
    54     self.title = [self.teamDictionary objectForKey:@"teamname"];
    52     self.title = self.teamName;
       
    53     [teamDict release];
       
    54     
    55     
    55     // load the images of the hat for aach hog
    56     // load the images of the hat for aach hog
    56     NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:[self.hogsList count]];
    57     NSArray *hogArray = [self.teamDictionary objectForKey:@"hedgehogs"];
    57     for (NSDictionary *hog in self.hogsList) {
    58     NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:[hogArray count]];
       
    59     for (NSDictionary *hog in hogArray) {
    58         NSString *hatFile = [[NSString alloc] initWithFormat:@"%@/Data/Graphics/Hats/%@.png",[[NSBundle mainBundle] resourcePath],[hog objectForKey:@"hat"]];
    60         NSString *hatFile = [[NSString alloc] initWithFormat:@"%@/Data/Graphics/Hats/%@.png",[[NSBundle mainBundle] resourcePath],[hog objectForKey:@"hat"]];
    59 
    61 
    60         UIImage *image = [[UIImage alloc] initWithContentsOfFile: hatFile];
    62         UIImage *image = [[UIImage alloc] initWithContentsOfFile: hatFile];
    61         CGRect firstSpriteArea = CGRectMake(0, 0, 32, 32);
    63         CGRect firstSpriteArea = CGRectMake(0, 0, 32, 32);
    62         CGImageRef cgImgage = CGImageCreateWithImageInRect([image CGImage], firstSpriteArea);
    64         CGImageRef cgImgage = CGImageCreateWithImageInRect([image CGImage], firstSpriteArea);
    65         UIImage *hatSprite = [[UIImage alloc] initWithCGImage:cgImgage];
    67         UIImage *hatSprite = [[UIImage alloc] initWithCGImage:cgImgage];
    66         [array addObject:hatSprite];
    68         [array addObject:hatSprite];
    67         CGImageRelease(cgImgage);
    69         CGImageRelease(cgImgage);
    68         [hatSprite release];
    70         [hatSprite release];
    69     }
    71     }
    70     self.hatList = array;
    72     self.hatArray = array;
    71     [array release];
    73     [array release];
       
    74     
       
    75     [self.tableView reloadData];
    72 }
    76 }
    73 
    77 
    74 /*
    78 /*
    75 - (void)viewDidAppear:(BOOL)animated {
    79 - (void)viewDidAppear:(BOOL)animated {
    76     [super viewDidAppear:animated];
    80     [super viewDidAppear:animated];
    77 }
    81 }
    78 */
    82 */
    79 /*
    83 
    80 - (void)viewWillDisappear:(BOOL)animated {
    84 -(void) setWriteNeeded {
       
    85     isWriteNeeded = YES;
       
    86 }
       
    87 
       
    88 // write to disk the team dictionary
       
    89 -(void) viewWillDisappear:(BOOL)animated {
    81     [super viewWillDisappear:animated];
    90     [super viewWillDisappear:animated];
    82 }
    91 
    83 */
    92     if (isWriteNeeded) {
       
    93         NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
       
    94         NSString *teamFile = [[NSString alloc] initWithFormat:@"%@/Teams/%@.plist",[paths objectAtIndex:0],self.title];
       
    95         [self.teamDictionary writeToFile: teamFile atomically:YES];
       
    96         [teamFile release];
       
    97 
       
    98         isWriteNeeded = NO;
       
    99     }
       
   100 }
       
   101 
    84 /*
   102 /*
    85 - (void)viewDidDisappear:(BOOL)animated {
   103 - (void)viewDidDisappear:(BOOL)animated {
    86     [super viewDidDisappear:animated];
   104     [super viewDidDisappear:animated];
    87 }
   105 }
    88 */
   106 */
   122     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
   140     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
   123     if (cell == nil) {
   141     if (cell == nil) {
   124         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
   142         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
   125                                        reuseIdentifier:CellIdentifier] autorelease];
   143                                        reuseIdentifier:CellIdentifier] autorelease];
   126     }
   144     }
   127     
   145     NSArray *hogArray;
   128     NSInteger row = [indexPath row];
   146     NSInteger row = [indexPath row];
   129     switch ([indexPath section]) {
   147     switch ([indexPath section]) {
   130         case 0:
   148         case 0:
   131             cell.textLabel.text = teamName;
   149             cell.textLabel.text = self.title;
       
   150             cell.imageView.image = nil;
   132             break;
   151             break;
   133         case 1:
   152         case 1:
   134             cell.textLabel.text = [[self.hogsList objectAtIndex:row] objectForKey:@"hogname"];
   153             hogArray = [self.teamDictionary objectForKey:@"hedgehogs"];
       
   154             cell.textLabel.text = [[hogArray objectAtIndex:row] objectForKey:@"hogname"];
   135             cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
   155             cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
   136             cell.imageView.image = [self.hatList objectAtIndex:row];
   156             cell.imageView.image = [self.hatArray objectAtIndex:row];
   137             break;
   157             break;
   138         case 2:
   158         case 2:
   139             cell.textLabel.text = [self.secondaryItems objectAtIndex:row];
   159             cell.textLabel.text = [self.secondaryItems objectAtIndex:row];
       
   160             cell.imageView.image = nil;
   140             break;
   161             break;
   141         default:
   162         default:
   142             break;
   163             break;
   143     }
   164     }
   144     
   165     
   186 */
   207 */
   187 
   208 
   188 
   209 
   189 #pragma mark -
   210 #pragma mark -
   190 #pragma mark Table view delegate
   211 #pragma mark Table view delegate
   191 
       
   192 -(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
   212 -(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
   193     if (1 == [indexPath section]) {
   213     if (1 == [indexPath section]) {
   194         if (nil == hogChildController) {
   214         if (nil == hogChildController) {
   195             hogChildController = [[HogHatViewController alloc] initWithStyle:UITableViewStyleGrouped];
   215             hogChildController = [[HogHatViewController alloc] initWithStyle:UITableViewStyleGrouped];
   196         }
   216         }
   197     
   217         
   198         hogChildController.hog = [hogsList objectAtIndex:[indexPath row]];
   218         // cache the dictionary file of the team, so that other controllers can modify it
   199         //NSLog(@"%@",hogChildController.hog);
   219         hogChildController.teamDictionary = self.teamDictionary;
       
   220         hogChildController.selectedHog = [indexPath row];
       
   221 
   200         [self.navigationController pushViewController:hogChildController animated:YES];
   222         [self.navigationController pushViewController:hogChildController animated:YES];
   201     }
   223     }
   202 }
   224 }
   203 
   225 
   204 
   226 
   205 #pragma mark -
   227 #pragma mark -
   206 #pragma mark Memory management
   228 #pragma mark Memory management
   207 
       
   208 -(void) didReceiveMemoryWarning {
   229 -(void) didReceiveMemoryWarning {
   209     // Releases the view if it doesn't have a superview.
   230     // Releases the view if it doesn't have a superview.
   210     [super didReceiveMemoryWarning];
   231     [super didReceiveMemoryWarning];
   211     // Relinquish ownership any cached data, images, etc that aren't in use.
   232     // Relinquish ownership any cached data, images, etc that aren't in use.
   212 }
   233 }
   213 
   234 
   214 -(void) viewDidUnload {
   235 -(void) viewDidUnload {
   215     self.hogsList = nil;
   236     self.hatArray = nil;
   216     self.secondaryItems = nil;
   237     self.secondaryItems = nil;
   217     self.teamName = nil;
   238     self.teamDictionary = nil;
       
   239     [super viewDidUnload];
   218 }
   240 }
   219 
   241 
   220 
   242 
   221 -(void) dealloc {
   243 -(void) dealloc {
   222     [secondaryItems release];
   244     [secondaryItems release];
   223     [hogsList release];
   245     [hatArray release];
   224     [teamName release];
   246     [teamDictionary release];
   225     [super dealloc];
   247     [super dealloc];
   226 }
   248 }
   227 
   249 
   228 
   250 
   229 @end
   251 @end