author | koda |
Thu, 08 Apr 2010 22:45:18 +0000 | |
changeset 3325 | 652a8ebdf667 |
parent 3317 | 198ec44b6d92 |
child 3328 | fe87c2242984 |
permissions | -rw-r--r-- |
3305 | 1 |
// |
2 |
// HogHatViewController.m |
|
3 |
// HedgewarsMobile |
|
4 |
// |
|
5 |
// Created by Vittorio on 02/04/10. |
|
6 |
// Copyright 2010 __MyCompanyName__. All rights reserved. |
|
7 |
// |
|
8 |
||
9 |
#import "HogHatViewController.h" |
|
3325 | 10 |
#import "CommodityFunctions.h" |
3305 | 11 |
|
12 |
||
13 |
@implementation HogHatViewController |
|
3315 | 14 |
@synthesize teamDictionary, hatArray, hatSprites, lastIndexPath, selectedHog; |
3305 | 15 |
|
3325 | 16 |
|
17 |
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { |
|
18 |
return (interfaceOrientation == UIInterfaceOrientationLandscapeRight); |
|
19 |
} |
|
20 |
||
3305 | 21 |
#pragma mark - |
22 |
#pragma mark View lifecycle |
|
23 |
- (void)viewDidLoad { |
|
24 |
[super viewDidLoad]; |
|
25 |
||
3315 | 26 |
// load all the hat file names and store them into hatArray |
3325 | 27 |
NSString *hatsDirectory = HATS_DIRECTORY(); |
28 |
NSArray *array = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:hatsDirectory error:NULL]; |
|
3315 | 29 |
self.hatArray = array; |
30 |
||
31 |
// load all the hat images from the previous array but save only the first sprite and store it in hatSprites |
|
32 |
NSMutableArray *spriteArray = [[NSMutableArray alloc] initWithCapacity:[hatArray count]]; |
|
3325 | 33 |
for (int i=0; i < [hatArray count]; i++) { |
34 |
NSString *hatFile = [[NSString alloc] initWithFormat:@"%@/%@", hatsDirectory,[hatArray objectAtIndex:i]]; |
|
3308 | 35 |
|
36 |
UIImage *image = [[UIImage alloc] initWithContentsOfFile: hatFile]; |
|
3316 | 37 |
[hatFile release]; |
3308 | 38 |
CGRect firstSpriteArea = CGRectMake(0, 0, 32, 32); |
39 |
CGImageRef cgImgage = CGImageCreateWithImageInRect([image CGImage], firstSpriteArea); |
|
40 |
[image release]; |
|
41 |
||
42 |
UIImage *hatSprite = [[UIImage alloc] initWithCGImage:cgImgage]; |
|
43 |
[spriteArray addObject:hatSprite]; |
|
44 |
CGImageRelease(cgImgage); |
|
45 |
[hatSprite release]; |
|
46 |
} |
|
47 |
self.hatSprites = spriteArray; |
|
48 |
[spriteArray release]; |
|
3305 | 49 |
} |
50 |
||
51 |
- (void)viewWillAppear:(BOOL)animated { |
|
52 |
[super viewWillAppear:animated]; |
|
3315 | 53 |
self.title = [[[teamDictionary objectForKey:@"hedgehogs"] objectAtIndex:selectedHog] objectForKey:@"hogname"]; |
54 |
||
3308 | 55 |
// this updates the hog name and its hat |
3305 | 56 |
[self.tableView reloadData]; |
3308 | 57 |
// this moves the tableview to the top |
3312 | 58 |
[self.tableView setContentOffset:CGPointMake(0,0) animated:NO]; |
3305 | 59 |
} |
60 |
||
61 |
/* |
|
62 |
- (void)viewDidAppear:(BOOL)animated { |
|
63 |
[super viewDidAppear:animated]; |
|
64 |
} |
|
65 |
*/ |
|
66 |
/* |
|
67 |
- (void)viewWillDisappear:(BOOL)animated { |
|
68 |
[super viewWillDisappear:animated]; |
|
69 |
} |
|
70 |
*/ |
|
71 |
/* |
|
72 |
- (void)viewDidDisappear:(BOOL)animated { |
|
73 |
[super viewDidDisappear:animated]; |
|
74 |
} |
|
75 |
*/ |
|
76 |
||
77 |
||
78 |
#pragma mark - |
|
79 |
#pragma mark Table view data source |
|
80 |
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView { |
|
81 |
return 2; |
|
82 |
} |
|
83 |
||
84 |
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
|
85 |
NSInteger rows; |
|
86 |
if (0 == section) |
|
87 |
rows = 1; |
|
88 |
else |
|
3315 | 89 |
rows = [self.hatArray count]; |
3305 | 90 |
return rows; |
91 |
} |
|
92 |
||
93 |
// Customize the appearance of table view cells. |
|
94 |
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
|
95 |
||
96 |
static NSString *CellIdentifier = @"Cell"; |
|
97 |
||
98 |
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; |
|
99 |
if (cell == nil) { |
|
100 |
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; |
|
101 |
} |
|
102 |
||
3325 | 103 |
NSDictionary *hog = [[self.teamDictionary objectForKey:@"hedgehogs"] objectAtIndex:selectedHog]; |
3305 | 104 |
if (0 == [indexPath section]) { |
3315 | 105 |
cell.textLabel.text = self.title; |
3305 | 106 |
cell.imageView.image = nil; |
107 |
cell.accessoryType = UITableViewCellAccessoryNone; |
|
108 |
} else { |
|
3315 | 109 |
cell.textLabel.text = [[hatArray objectAtIndex:[indexPath row]] stringByDeletingPathExtension]; |
3305 | 110 |
if ([cell.textLabel.text isEqualToString:[hog objectForKey:@"hat"]]) { |
111 |
cell.accessoryType = UITableViewCellAccessoryCheckmark; |
|
3315 | 112 |
self.lastIndexPath = indexPath; |
3305 | 113 |
} else { |
114 |
cell.accessoryType = UITableViewCellAccessoryNone; |
|
115 |
} |
|
116 |
||
3308 | 117 |
cell.imageView.image = [hatSprites objectAtIndex:[indexPath row]]; |
3305 | 118 |
} |
119 |
||
120 |
return cell; |
|
121 |
} |
|
122 |
||
123 |
||
124 |
/* |
|
125 |
// Override to support conditional editing of the table view. |
|
126 |
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { |
|
127 |
// Return NO if you do not want the specified item to be editable. |
|
128 |
return YES; |
|
129 |
} |
|
130 |
*/ |
|
131 |
||
132 |
||
133 |
/* |
|
134 |
// Override to support editing the table view. |
|
135 |
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { |
|
136 |
||
137 |
if (editingStyle == UITableViewCellEditingStyleDelete) { |
|
138 |
// Delete the row from the data source |
|
139 |
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES]; |
|
140 |
} |
|
141 |
else if (editingStyle == UITableViewCellEditingStyleInsert) { |
|
142 |
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view |
|
143 |
} |
|
144 |
} |
|
145 |
*/ |
|
146 |
||
147 |
||
148 |
/* |
|
149 |
// Override to support rearranging the table view. |
|
150 |
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath { |
|
151 |
} |
|
152 |
*/ |
|
153 |
||
154 |
||
155 |
/* |
|
156 |
// Override to support conditional rearranging of the table view. |
|
157 |
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath { |
|
158 |
// Return NO if you do not want the item to be re-orderable. |
|
159 |
return YES; |
|
160 |
} |
|
161 |
*/ |
|
162 |
||
163 |
||
164 |
#pragma mark - |
|
165 |
#pragma mark Table view delegate |
|
3315 | 166 |
- (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
167 |
if (1 == [indexPath section]) { |
|
168 |
int newRow = [indexPath row]; |
|
169 |
int oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1; |
|
170 |
||
171 |
if (newRow != oldRow) { |
|
172 |
// if the two selected rows differ update data on the hog dictionary and reload table content |
|
173 |
NSDictionary *oldHog = [[teamDictionary objectForKey:@"hedgehogs"] objectAtIndex:selectedHog]; |
|
174 |
||
175 |
NSMutableDictionary *newHog = [[NSMutableDictionary alloc] initWithDictionary: oldHog]; |
|
176 |
[newHog setObject:[[hatArray objectAtIndex:newRow] stringByDeletingPathExtension] forKey:@"hat"]; |
|
177 |
[[teamDictionary objectForKey:@"hedgehogs"] replaceObjectAtIndex:selectedHog withObject:newHog]; |
|
178 |
[newHog release]; |
|
179 |
||
180 |
// tell our boss to write this new stuff on disk |
|
181 |
[[NSNotificationCenter defaultCenter] postNotificationName:@"setWriteNeedTeams" object:nil]; |
|
182 |
[self.tableView reloadData]; |
|
183 |
||
184 |
self.lastIndexPath = indexPath; |
|
185 |
[self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone]; |
|
186 |
} |
|
187 |
[self.tableView deselectRowAtIndexPath:indexPath animated:YES]; |
|
3317
198ec44b6d92
minor tweaks, icon for ipad, merged overlayviewcontroller, pop viewcontroller when selected a hat
koda
parents:
3316
diff
changeset
|
188 |
[self.navigationController popViewControllerAnimated:YES]; |
3315 | 189 |
} |
3305 | 190 |
} |
191 |
||
192 |
||
193 |
#pragma mark - |
|
194 |
#pragma mark Memory management |
|
195 |
- (void)didReceiveMemoryWarning { |
|
196 |
// Releases the view if it doesn't have a superview. |
|
197 |
[super didReceiveMemoryWarning]; |
|
198 |
// Relinquish ownership any cached data, images, etc that aren't in use. |
|
199 |
} |
|
200 |
||
201 |
- (void)viewDidUnload { |
|
202 |
[super viewDidUnload]; |
|
3315 | 203 |
self.lastIndexPath = nil; |
204 |
self.hatSprites = nil; |
|
205 |
self.teamDictionary = nil; |
|
206 |
self.hatArray = nil; |
|
3305 | 207 |
} |
208 |
||
209 |
- (void)dealloc { |
|
3315 | 210 |
[hatArray release]; |
211 |
[teamDictionary release]; |
|
212 |
[hatSprites release]; |
|
213 |
[lastIndexPath release]; |
|
3305 | 214 |
[super dealloc]; |
215 |
} |
|
216 |
||
217 |
||
218 |
@end |
|
219 |