author | smxx |
Sat, 05 Jun 2010 20:49:51 +0000 | |
changeset 3499 | 66eba4e41b91 |
parent 3490 | 016b3172b645 |
permissions | -rw-r--r-- |
3325 | 1 |
// |
3352 | 2 |
// FortsViewController.m |
3325 | 3 |
// HedgewarsMobile |
4 |
// |
|
5 |
// Created by Vittorio on 08/04/10. |
|
6 |
// Copyright 2010 __MyCompanyName__. All rights reserved. |
|
7 |
// |
|
8 |
||
9 |
#import "FortsViewController.h" |
|
10 |
#import "CommodityFunctions.h" |
|
3352 | 11 |
#import "UIImageExtra.h" |
3325 | 12 |
|
13 |
@implementation FortsViewController |
|
3374
0d522416d97f
lazy loading for all the tables with images (might affect performance but ui feels much more responsive)
koda
parents:
3357
diff
changeset
|
14 |
@synthesize teamDictionary, fortArray, lastIndexPath; |
3325 | 15 |
|
16 |
||
3335 | 17 |
-(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation { |
18 |
return rotationManager(interfaceOrientation); |
|
3325 | 19 |
} |
20 |
||
3335 | 21 |
|
3325 | 22 |
#pragma mark - |
23 |
#pragma mark View lifecycle |
|
3374
0d522416d97f
lazy loading for all the tables with images (might affect performance but ui feels much more responsive)
koda
parents:
3357
diff
changeset
|
24 |
-(void) viewDidLoad { |
3325 | 25 |
[super viewDidLoad]; |
26 |
||
3374
0d522416d97f
lazy loading for all the tables with images (might affect performance but ui feels much more responsive)
koda
parents:
3357
diff
changeset
|
27 |
NSArray *directoryContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:FORTS_DIRECTORY() error:NULL]; |
3325 | 28 |
NSMutableArray *filteredContents = [[NSMutableArray alloc] initWithCapacity:([directoryContents count] / 2)]; |
29 |
// we need to remove the double entries and the L.png suffix |
|
30 |
for (int i = 0; i < [directoryContents count]; i++) { |
|
31 |
if (i % 2) { |
|
32 |
NSString *currentName = [directoryContents objectAtIndex:i]; |
|
33 |
NSString *correctName = [currentName substringToIndex:([currentName length] - 5)]; |
|
34 |
[filteredContents addObject:correctName]; |
|
35 |
} |
|
36 |
} |
|
37 |
self.fortArray = filteredContents; |
|
38 |
[filteredContents release]; |
|
39 |
||
3374
0d522416d97f
lazy loading for all the tables with images (might affect performance but ui feels much more responsive)
koda
parents:
3357
diff
changeset
|
40 |
/* |
3325 | 41 |
// this creates a scaled down version of the image |
42 |
NSMutableArray *spriteArray = [[NSMutableArray alloc] initWithCapacity:[fortArray count]]; |
|
43 |
for (NSString *fortName in fortArray) { |
|
44 |
NSString *fortFile = [[NSString alloc] initWithFormat:@"%@/%@L.png", fortsDirectory, fortName]; |
|
45 |
UIImage *fortSprite = [[UIImage alloc] initWithContentsOfFile:fortFile]; |
|
46 |
[fortFile release]; |
|
47 |
[spriteArray addObject:[fortSprite scaleToSize:CGSizeMake(196,196)]]; |
|
48 |
[fortSprite release]; |
|
49 |
} |
|
50 |
self.fortSprites = spriteArray; |
|
51 |
[spriteArray release]; |
|
3374
0d522416d97f
lazy loading for all the tables with images (might affect performance but ui feels much more responsive)
koda
parents:
3357
diff
changeset
|
52 |
*/ |
3357 | 53 |
|
54 |
// statically set row height instead of using delegate method for performance reasons |
|
55 |
self.tableView.rowHeight = 200; |
|
3325 | 56 |
} |
57 |
||
58 |
||
59 |
- (void)viewWillAppear:(BOOL)animated { |
|
60 |
[super viewWillAppear:animated]; |
|
61 |
[self.tableView reloadData]; |
|
62 |
[self.tableView setContentOffset:CGPointMake(0,0) animated:NO]; |
|
63 |
} |
|
64 |
||
65 |
||
66 |
#pragma mark - |
|
67 |
#pragma mark Table view data source |
|
3374
0d522416d97f
lazy loading for all the tables with images (might affect performance but ui feels much more responsive)
koda
parents:
3357
diff
changeset
|
68 |
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView { |
3325 | 69 |
return 1; |
70 |
} |
|
71 |
||
3374
0d522416d97f
lazy loading for all the tables with images (might affect performance but ui feels much more responsive)
koda
parents:
3357
diff
changeset
|
72 |
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
0d522416d97f
lazy loading for all the tables with images (might affect performance but ui feels much more responsive)
koda
parents:
3357
diff
changeset
|
73 |
return [self.fortArray count]; |
3325 | 74 |
} |
75 |
||
76 |
// Customize the appearance of table view cells. |
|
3374
0d522416d97f
lazy loading for all the tables with images (might affect performance but ui feels much more responsive)
koda
parents:
3357
diff
changeset
|
77 |
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
3325 | 78 |
static NSString *CellIdentifier = @"Cell"; |
79 |
||
80 |
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; |
|
81 |
if (cell == nil) { |
|
82 |
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle |
|
83 |
reuseIdentifier:CellIdentifier] autorelease]; |
|
84 |
} |
|
85 |
||
3374
0d522416d97f
lazy loading for all the tables with images (might affect performance but ui feels much more responsive)
koda
parents:
3357
diff
changeset
|
86 |
NSString *fortName = [fortArray objectAtIndex:[indexPath row]]; |
0d522416d97f
lazy loading for all the tables with images (might affect performance but ui feels much more responsive)
koda
parents:
3357
diff
changeset
|
87 |
cell.textLabel.text = fortName; |
0d522416d97f
lazy loading for all the tables with images (might affect performance but ui feels much more responsive)
koda
parents:
3357
diff
changeset
|
88 |
|
0d522416d97f
lazy loading for all the tables with images (might affect performance but ui feels much more responsive)
koda
parents:
3357
diff
changeset
|
89 |
// this creates a scaled down version of the image |
0d522416d97f
lazy loading for all the tables with images (might affect performance but ui feels much more responsive)
koda
parents:
3357
diff
changeset
|
90 |
// TODO: create preview files, scaling is way too slow! |
0d522416d97f
lazy loading for all the tables with images (might affect performance but ui feels much more responsive)
koda
parents:
3357
diff
changeset
|
91 |
NSString *fortFile = [[NSString alloc] initWithFormat:@"%@/%@L.png", FORTS_DIRECTORY(), fortName]; |
0d522416d97f
lazy loading for all the tables with images (might affect performance but ui feels much more responsive)
koda
parents:
3357
diff
changeset
|
92 |
UIImage *fortSprite = [[UIImage alloc] initWithContentsOfFile:fortFile]; |
0d522416d97f
lazy loading for all the tables with images (might affect performance but ui feels much more responsive)
koda
parents:
3357
diff
changeset
|
93 |
[fortFile release]; |
0d522416d97f
lazy loading for all the tables with images (might affect performance but ui feels much more responsive)
koda
parents:
3357
diff
changeset
|
94 |
cell.imageView.image = [fortSprite scaleToSize:CGSizeMake(196,196)]; |
0d522416d97f
lazy loading for all the tables with images (might affect performance but ui feels much more responsive)
koda
parents:
3357
diff
changeset
|
95 |
[fortSprite release]; |
0d522416d97f
lazy loading for all the tables with images (might affect performance but ui feels much more responsive)
koda
parents:
3357
diff
changeset
|
96 |
|
3325 | 97 |
cell.detailTextLabel.text = @"Insert funny description here"; |
98 |
if ([cell.textLabel.text isEqualToString:[self.teamDictionary objectForKey:@"fort"]]) { |
|
99 |
cell.accessoryType = UITableViewCellAccessoryCheckmark; |
|
100 |
self.lastIndexPath = indexPath; |
|
101 |
} else { |
|
102 |
cell.accessoryType = UITableViewCellAccessoryNone; |
|
103 |
} |
|
104 |
||
105 |
return cell; |
|
106 |
} |
|
107 |
||
108 |
||
109 |
#pragma mark - |
|
110 |
#pragma mark Table view delegate |
|
3374
0d522416d97f
lazy loading for all the tables with images (might affect performance but ui feels much more responsive)
koda
parents:
3357
diff
changeset
|
111 |
-(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
3325 | 112 |
int newRow = [indexPath row]; |
113 |
int oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1; |
|
114 |
||
115 |
if (newRow != oldRow) { |
|
116 |
// if the two selected rows differ update data on the hog dictionary and reload table content |
|
117 |
[self.teamDictionary setValue:[fortArray objectAtIndex:newRow] forKey:@"fort"]; |
|
118 |
||
119 |
// tell our boss to write this new stuff on disk |
|
120 |
[[NSNotificationCenter defaultCenter] postNotificationName:@"setWriteNeedTeams" object:nil]; |
|
3374
0d522416d97f
lazy loading for all the tables with images (might affect performance but ui feels much more responsive)
koda
parents:
3357
diff
changeset
|
121 |
|
0d522416d97f
lazy loading for all the tables with images (might affect performance but ui feels much more responsive)
koda
parents:
3357
diff
changeset
|
122 |
UITableViewCell *newCell = [aTableView cellForRowAtIndexPath:indexPath]; |
0d522416d97f
lazy loading for all the tables with images (might affect performance but ui feels much more responsive)
koda
parents:
3357
diff
changeset
|
123 |
newCell.accessoryType = UITableViewCellAccessoryCheckmark; |
0d522416d97f
lazy loading for all the tables with images (might affect performance but ui feels much more responsive)
koda
parents:
3357
diff
changeset
|
124 |
UITableViewCell *oldCell = [aTableView cellForRowAtIndexPath:lastIndexPath]; |
0d522416d97f
lazy loading for all the tables with images (might affect performance but ui feels much more responsive)
koda
parents:
3357
diff
changeset
|
125 |
oldCell.accessoryType = UITableViewCellAccessoryNone; |
3325 | 126 |
self.lastIndexPath = indexPath; |
3374
0d522416d97f
lazy loading for all the tables with images (might affect performance but ui feels much more responsive)
koda
parents:
3357
diff
changeset
|
127 |
[aTableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone]; |
3325 | 128 |
} |
3374
0d522416d97f
lazy loading for all the tables with images (might affect performance but ui feels much more responsive)
koda
parents:
3357
diff
changeset
|
129 |
[aTableView deselectRowAtIndexPath:indexPath animated:YES]; |
3325 | 130 |
[self.navigationController popViewControllerAnimated:YES]; |
131 |
} |
|
132 |
||
133 |
||
134 |
#pragma mark - |
|
135 |
#pragma mark Memory management |
|
136 |
-(void) didReceiveMemoryWarning { |
|
137 |
// Releases the view if it doesn't have a superview. |
|
138 |
[super didReceiveMemoryWarning]; |
|
139 |
// Relinquish ownership any cached data, images, etc that aren't in use. |
|
140 |
} |
|
141 |
||
142 |
-(void) viewDidUnload { |
|
143 |
self.teamDictionary = nil; |
|
144 |
self.lastIndexPath = nil; |
|
145 |
self.fortArray = nil; |
|
146 |
[super viewDidUnload]; |
|
3490 | 147 |
MSG_DIDUNLOAD(); |
3325 | 148 |
} |
149 |
||
150 |
||
151 |
- (void)dealloc { |
|
152 |
[teamDictionary release]; |
|
153 |
[lastIndexPath release]; |
|
154 |
[fortArray release]; |
|
3374
0d522416d97f
lazy loading for all the tables with images (might affect performance but ui feels much more responsive)
koda
parents:
3357
diff
changeset
|
155 |
// [fortSprites release]; |
3325 | 156 |
[super dealloc]; |
157 |
} |
|
158 |
||
159 |
||
160 |
@end |
|
161 |