cocoaTouch/SingleTeamViewController.m
changeset 3328 fe87c2242984
parent 3325 652a8ebdf667
child 3329 d8e41ee0b3ae
equal deleted inserted replaced
3327:ac0ddbb205fe 3328:fe87c2242984
    36     
    36     
    37     // insert controllers here
    37     // insert controllers here
    38     NSMutableArray *controllersArray = [[NSMutableArray alloc] initWithCapacity:[secondaryItems count]];
    38     NSMutableArray *controllersArray = [[NSMutableArray alloc] initWithCapacity:[secondaryItems count]];
    39     
    39     
    40     FlagsViewController *flagsViewController = [[FlagsViewController alloc] initWithStyle:UITableViewStyleGrouped];
    40     FlagsViewController *flagsViewController = [[FlagsViewController alloc] initWithStyle:UITableViewStyleGrouped];
    41     flagsViewController.teamDictionary = self.teamDictionary;
       
    42     [controllersArray addObject:flagsViewController];
    41     [controllersArray addObject:flagsViewController];
    43     [flagsViewController release];
    42     [flagsViewController release];
    44     
    43     
    45     FortsViewController *fortsViewController = [[FortsViewController alloc] initWithStyle:UITableViewStyleGrouped];
    44     FortsViewController *fortsViewController = [[FortsViewController alloc] initWithStyle:UITableViewStyleGrouped];
    46     fortsViewController.teamDictionary = self.teamDictionary;
       
    47     [controllersArray addObject:fortsViewController];
    45     [controllersArray addObject:fortsViewController];
    48     [fortsViewController release];
    46     [fortsViewController release];
    49     
    47     
    50     self.secondaryControllers = controllersArray;
    48     self.secondaryControllers = controllersArray;
    51     [controllersArray release];
    49     [controllersArray release];
    56 }
    54 }
    57 
    55 
    58 - (void)viewWillAppear:(BOOL)animated {
    56 - (void)viewWillAppear:(BOOL)animated {
    59     [super viewWillAppear:animated];
    57     [super viewWillAppear:animated];
    60     
    58     
    61     self.title = [self.teamDictionary objectForKey:@"teamname"];
    59     // load data about the team and extract info
    62     
    60     NSString *teamFile = [[NSString alloc] initWithFormat:@"%@/%@.plist",TEAMS_DIRECTORY(),self.title];
       
    61     if (isWriteNeeded) {
       
    62         [self.teamDictionary writeToFile:teamFile atomically:YES];
       
    63         NSLog(@"writing: %@",teamDictionary);
       
    64         isWriteNeeded = NO;
       
    65     }
       
    66     
       
    67 	NSMutableDictionary *teamDict = [[NSMutableDictionary alloc] initWithContentsOfFile:teamFile];
       
    68     self.teamDictionary = teamDict;
       
    69     [teamDict release];
       
    70     
       
    71 	[teamFile release];
       
    72         
    63     // load the images of the hat for aach hog
    73     // load the images of the hat for aach hog
    64     NSArray *hogArray = [self.teamDictionary objectForKey:@"hedgehogs"];
    74     NSArray *hogArray = [self.teamDictionary objectForKey:@"hedgehogs"];
    65     NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:[hogArray count]];
    75     NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:[hogArray count]];
    66     for (NSDictionary *hog in hogArray) {
    76     for (NSDictionary *hog in hogArray) {
    67         NSString *hatFile = [[NSString alloc] initWithFormat:@"%@/%@.png", HATS_DIRECTORY(), [hog objectForKey:@"hat"]];
    77         NSString *hatFile = [[NSString alloc] initWithFormat:@"%@/%@.png",HATS_DIRECTORY(),[hog objectForKey:@"hat"]];
    68 
    78 
    69         UIImage *image = [[UIImage alloc] initWithContentsOfFile: hatFile];
    79         UIImage *image = [[UIImage alloc] initWithContentsOfFile: hatFile];
    70         [hatFile release];
    80         [hatFile release];
    71         CGRect firstSpriteArea = CGRectMake(0, 0, 32, 32);
    81         CGRect firstSpriteArea = CGRectMake(0, 0, 32, 32);
    72         CGImageRef cgImgage = CGImageCreateWithImageInRect([image CGImage], firstSpriteArea);
    82         CGImageRef cgImgage = CGImageCreateWithImageInRect([image CGImage], firstSpriteArea);
    81     [array release];
    91     [array release];
    82     
    92     
    83     [self.tableView reloadData];
    93     [self.tableView reloadData];
    84 }
    94 }
    85 
    95 
       
    96 -(void) viewWillDisappear:(BOOL)animated {
       
    97 	[super viewWillDisappear:animated];
       
    98 	
       
    99 	NSString *teamFile = [[NSString alloc] initWithFormat:@"%@/%@.plist",TEAMS_DIRECTORY(),self.title];
       
   100     if (isWriteNeeded) {
       
   101         [self.teamDictionary writeToFile:teamFile atomically:YES];
       
   102         NSLog(@"writing: %@",teamDictionary);
       
   103         isWriteNeeded = NO;
       
   104     }
       
   105 
       
   106 	[teamFile release];
       
   107 }
    86 // needed by other classes to warn about a user change
   108 // needed by other classes to warn about a user change
    87 -(void) setWriteNeeded {
   109 -(void) setWriteNeeded {
    88     isWriteNeeded = YES;
   110     isWriteNeeded = YES;
    89 }
   111 }
    90 
       
    91 // write to disk the team dictionary
       
    92 -(void) viewWillDisappear:(BOOL)animated {
       
    93     [super viewWillDisappear:animated];
       
    94 
       
    95     if (isWriteNeeded) {
       
    96         NSString *teamFile = [[NSString alloc] initWithFormat:@"%@/%@.plist",TEAMS_DIRECTORY(),self.title];
       
    97         [self.teamDictionary writeToFile:teamFile atomically:YES];
       
    98         [teamFile release];
       
    99         NSLog(@"writing: %@",teamDictionary);
       
   100         isWriteNeeded = NO;
       
   101     }
       
   102 }
       
   103 
       
   104 
   112 
   105 #pragma mark -
   113 #pragma mark -
   106 #pragma mark Table view data source
   114 #pragma mark Table view data source
   107 -(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
   115 -(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
   108     return 3;
   116     return 3;
   133     if (cell == nil) {
   141     if (cell == nil) {
   134         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
   142         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
   135                                        reuseIdentifier:CellIdentifier] autorelease];
   143                                        reuseIdentifier:CellIdentifier] autorelease];
   136     }
   144     }
   137 
   145 
   138     cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
       
   139 
   146 
   140     NSArray *hogArray;
   147     NSArray *hogArray;
   141     NSInteger row = [indexPath row];
   148     NSInteger row = [indexPath row];
   142     switch ([indexPath section]) {
   149     switch ([indexPath section]) {
   143         case 0:
   150         case 0:
   144             cell.textLabel.text = self.title;
   151             cell.textLabel.text = self.title;
   145             cell.imageView.image = nil;
   152             cell.imageView.image = nil;
       
   153             cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
   146             break;
   154             break;
   147         case 1:
   155         case 1:
   148             hogArray = [self.teamDictionary objectForKey:@"hedgehogs"];
   156             hogArray = [self.teamDictionary objectForKey:@"hedgehogs"];
   149             cell.textLabel.text = [[hogArray objectAtIndex:row] objectForKey:@"hogname"];
   157             cell.textLabel.text = [[hogArray objectAtIndex:row] objectForKey:@"hogname"];
   150             cell.imageView.image = [self.hatArray objectAtIndex:row];
   158             cell.imageView.image = [self.hatArray objectAtIndex:row];
       
   159             cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
   151             break;
   160             break;
   152         case 2:
   161         case 2:
   153             cell.textLabel.text = [self.secondaryItems objectAtIndex:row];
   162             cell.textLabel.text = [self.secondaryItems objectAtIndex:row];
       
   163             cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
   154             switch (row) {
   164             switch (row) {
   155                 case 3: // flags
   165                 case 3: // flags
   156                     cell.imageView.image = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%@.png",
   166                     cell.imageView.image = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%@.png",
   157                                                                              FLAGS_DIRECTORY(),[teamDictionary objectForKey:@"flag"]]];
   167                                                                              FLAGS_DIRECTORY(),[teamDictionary objectForKey:@"flag"]]];
   158                     break;
   168                     break;
   175     // Return NO if you do not want the specified item to be editable.
   185     // Return NO if you do not want the specified item to be editable.
   176     return YES;
   186     return YES;
   177 }
   187 }
   178 */
   188 */
   179 
   189 
   180 
       
   181 /*
   190 /*
   182 // Override to support editing the table view.
   191 // Override to support editing the table view.
   183 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
   192 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
       
   193  forRowAtIndexPath:(NSIndexPath *)indexPath {
   184     
   194     
   185     if (editingStyle == UITableViewCellEditingStyleDelete) {
   195     if (editingStyle == UITableViewCellEditingStyleDelete) {
   186         // Delete the row from the data source
   196         // Delete the row from the data source
   187         [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
   197         [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
   188     }   
   198     }   
   190         // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
   200         // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
   191     }   
   201     }   
   192 }
   202 }
   193 */
   203 */
   194 
   204 
   195 
       
   196 /*
   205 /*
   197 // Override to support rearranging the table view.
   206 // Override to support rearranging the table view.
   198 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
   207 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
   199 }
   208 }
   200 */
   209 */
   213 #pragma mark Table view delegate
   222 #pragma mark Table view delegate
   214 -(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
   223 -(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
   215     NSInteger row = [indexPath row];
   224     NSInteger row = [indexPath row];
   216     UITableViewController *nextController;
   225     UITableViewController *nextController;
   217     if (1 == [indexPath section]) {
   226     if (1 == [indexPath section]) {
   218         if (nil == hogChildController) {
   227         //TODO: hog name pref, causes segfault
   219             hogChildController = [[HogHatViewController alloc] initWithStyle:UITableViewStyleGrouped];
       
   220         }
       
   221         
       
   222         // cache the dictionary file of the team, so that other controllers can modify it
       
   223         hogChildController.teamDictionary = self.teamDictionary;
       
   224         hogChildController.selectedHog = row;
       
   225 
       
   226         nextController = hogChildController;
       
   227     }
   228     }
   228     if (2 == [indexPath section]) {
   229     if (2 == [indexPath section]) {
   229         //TODO: this part should be rewrittend with lazy loading instead of an array of controllers
   230         //TODO: this part should be rewrittend with lazy loading instead of an array of controllers
   230         nextController = [secondaryControllers objectAtIndex:row%2 ];              //TODO: fix the objectAtIndex
   231         nextController = [secondaryControllers objectAtIndex:row%2 ];              //TODO: fix the objectAtIndex
   231         nextController.title = [secondaryItems objectAtIndex:row];
   232         nextController.title = [secondaryItems objectAtIndex:row];
       
   233         [nextController setTeamDictionary:teamDictionary];
   232     }
   234     }
   233     [self.navigationController pushViewController:nextController animated:YES];
   235     [self.navigationController pushViewController:nextController animated:YES];
       
   236 }
       
   237 
       
   238 -(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath {
       
   239     if (nil == hogChildController) {
       
   240         hogChildController = [[HogHatViewController alloc] initWithStyle:UITableViewStyleGrouped];
       
   241     }
       
   242     
       
   243     // cache the dictionary file of the team, so that other controllers can modify it
       
   244     hogChildController.teamDictionary = self.teamDictionary;
       
   245     hogChildController.selectedHog = [indexPath row];
       
   246     
       
   247     [self.navigationController pushViewController:hogChildController animated:YES];
   234 }
   248 }
   235 
   249 
   236 
   250 
   237 #pragma mark -
   251 #pragma mark -
   238 #pragma mark Memory management
   252 #pragma mark Memory management