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