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 } |
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 |