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"
|
|
10 |
|
|
11 |
|
|
12 |
@implementation HogHatViewController
|
3308
|
13 |
@synthesize hatList, hatSprites, hog;
|
3305
|
14 |
|
|
15 |
#pragma mark -
|
|
16 |
#pragma mark View lifecycle
|
|
17 |
|
|
18 |
|
|
19 |
- (void)viewDidLoad {
|
|
20 |
[super viewDidLoad];
|
|
21 |
|
|
22 |
//NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
|
|
23 |
NSString *hatPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingString:@"/Data/Graphics/Hats/"];
|
|
24 |
NSArray *array = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:hatPath
|
|
25 |
error:NULL];
|
|
26 |
self.hatList = array;
|
3308
|
27 |
NSMutableArray *spriteArray = [[NSMutableArray alloc] initWithCapacity:[hatList count]];
|
|
28 |
for (int i=0; i< [hatList count]; i++) {
|
|
29 |
NSString *hatFile = [hatPath stringByAppendingString:[hatList objectAtIndex:i]];
|
|
30 |
|
|
31 |
UIImage *image = [[UIImage alloc] initWithContentsOfFile: hatFile];
|
|
32 |
CGRect firstSpriteArea = CGRectMake(0, 0, 32, 32);
|
|
33 |
CGImageRef cgImgage = CGImageCreateWithImageInRect([image CGImage], firstSpriteArea);
|
|
34 |
[image release];
|
|
35 |
|
|
36 |
UIImage *hatSprite = [[UIImage alloc] initWithCGImage:cgImgage];
|
|
37 |
[spriteArray addObject:hatSprite];
|
|
38 |
CGImageRelease(cgImgage);
|
|
39 |
[hatSprite release];
|
|
40 |
}
|
|
41 |
self.hatSprites = spriteArray;
|
|
42 |
[spriteArray release];
|
3305
|
43 |
//NSLog(@"%@", hatList);
|
|
44 |
}
|
|
45 |
|
|
46 |
- (void)viewWillAppear:(BOOL)animated {
|
|
47 |
[super viewWillAppear:animated];
|
|
48 |
self.title = [hog objectForKey:@"hogname"];
|
3308
|
49 |
// this updates the hog name and its hat
|
3305
|
50 |
[self.tableView reloadData];
|
3308
|
51 |
// this moves the tableview to the top
|
|
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 |
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
|
|
72 |
// Override to allow orientations other than the default portrait orientation.
|
|
73 |
return YES;
|
|
74 |
}
|
|
75 |
|
|
76 |
|
|
77 |
#pragma mark -
|
|
78 |
#pragma mark Table view data source
|
|
79 |
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
|
|
80 |
return 2;
|
|
81 |
}
|
|
82 |
|
|
83 |
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
|
|
84 |
NSInteger rows;
|
|
85 |
if (0 == section)
|
|
86 |
rows = 1;
|
|
87 |
else
|
|
88 |
rows = [self.hatList count];
|
|
89 |
return rows;
|
|
90 |
}
|
|
91 |
|
|
92 |
// Customize the appearance of table view cells.
|
|
93 |
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
|
|
94 |
|
|
95 |
static NSString *CellIdentifier = @"Cell";
|
|
96 |
|
|
97 |
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
|
|
98 |
if (cell == nil) {
|
|
99 |
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
|
|
100 |
}
|
|
101 |
|
|
102 |
if (0 == [indexPath section]) {
|
|
103 |
cell.textLabel.text = [hog objectForKey:@"hogname"];
|
|
104 |
cell.imageView.image = nil;
|
|
105 |
cell.accessoryType = UITableViewCellAccessoryNone;
|
|
106 |
} else {
|
|
107 |
cell.textLabel.text = [[hatList objectAtIndex:[indexPath row]] stringByDeletingPathExtension];
|
|
108 |
if ([cell.textLabel.text isEqualToString:[hog objectForKey:@"hat"]]) {
|
|
109 |
cell.accessoryType = UITableViewCellAccessoryCheckmark;
|
|
110 |
} else {
|
|
111 |
cell.accessoryType = UITableViewCellAccessoryNone;
|
|
112 |
}
|
|
113 |
|
3308
|
114 |
cell.imageView.image = [hatSprites objectAtIndex:[indexPath row]];
|
3305
|
115 |
}
|
|
116 |
|
|
117 |
return cell;
|
|
118 |
}
|
|
119 |
|
|
120 |
|
|
121 |
/*
|
|
122 |
// Override to support conditional editing of the table view.
|
|
123 |
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
|
|
124 |
// Return NO if you do not want the specified item to be editable.
|
|
125 |
return YES;
|
|
126 |
}
|
|
127 |
*/
|
|
128 |
|
|
129 |
|
|
130 |
/*
|
|
131 |
// Override to support editing the table view.
|
|
132 |
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
|
|
133 |
|
|
134 |
if (editingStyle == UITableViewCellEditingStyleDelete) {
|
|
135 |
// Delete the row from the data source
|
|
136 |
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
|
|
137 |
}
|
|
138 |
else if (editingStyle == UITableViewCellEditingStyleInsert) {
|
|
139 |
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
|
|
140 |
}
|
|
141 |
}
|
|
142 |
*/
|
|
143 |
|
|
144 |
|
|
145 |
/*
|
|
146 |
// Override to support rearranging the table view.
|
|
147 |
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
|
|
148 |
}
|
|
149 |
*/
|
|
150 |
|
|
151 |
|
|
152 |
/*
|
|
153 |
// Override to support conditional rearranging of the table view.
|
|
154 |
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
|
|
155 |
// Return NO if you do not want the item to be re-orderable.
|
|
156 |
return YES;
|
|
157 |
}
|
|
158 |
*/
|
|
159 |
|
|
160 |
|
|
161 |
#pragma mark -
|
|
162 |
#pragma mark Table view delegate
|
|
163 |
|
|
164 |
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
|
|
165 |
// Navigation logic may go here. Create and push another view controller.
|
|
166 |
/*
|
|
167 |
<#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
|
|
168 |
// ...
|
|
169 |
// Pass the selected object to the new view controller.
|
|
170 |
[self.navigationController pushViewController:detailViewController animated:YES];
|
|
171 |
[detailViewController release];
|
|
172 |
*/
|
|
173 |
}
|
|
174 |
|
|
175 |
|
|
176 |
#pragma mark -
|
|
177 |
#pragma mark Memory management
|
|
178 |
|
|
179 |
- (void)didReceiveMemoryWarning {
|
|
180 |
// Releases the view if it doesn't have a superview.
|
|
181 |
[super didReceiveMemoryWarning];
|
|
182 |
|
|
183 |
// Relinquish ownership any cached data, images, etc that aren't in use.
|
|
184 |
}
|
|
185 |
|
|
186 |
- (void)viewDidUnload {
|
|
187 |
[super viewDidUnload];
|
|
188 |
self.hatList = nil;
|
|
189 |
self.hog = nil;
|
|
190 |
// Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
|
|
191 |
// For example: self.myOutlet = nil;
|
|
192 |
}
|
|
193 |
|
|
194 |
|
|
195 |
- (void)dealloc {
|
|
196 |
[hog release];
|
|
197 |
[hatList release];
|
|
198 |
[super dealloc];
|
|
199 |
}
|
|
200 |
|
|
201 |
|
|
202 |
@end
|
|
203 |
|