75 |
75 |
76 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; |
76 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; |
77 if (cell == nil) |
77 if (cell == nil) |
78 cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; |
78 cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; |
79 |
79 |
80 NSDictionary *hog = [[self.teamDictionary objectForKey:@"hedgehogs"] objectAtIndex:selectedHog]; |
80 NSDictionary *hog = [[self.teamDictionary objectForKey:@"hedgehogs"] objectAtIndex:self.selectedHog]; |
81 NSString *hat = [hatArray objectAtIndex:[indexPath row]]; |
81 NSString *hat = [self.hatArray objectAtIndex:[indexPath row]]; |
82 cell.textLabel.text = [hat stringByDeletingPathExtension]; |
82 cell.textLabel.text = [hat stringByDeletingPathExtension]; |
83 |
83 |
84 NSString *hatFile = [[NSString alloc] initWithFormat:@"%@/%@", HATS_DIRECTORY(), hat]; |
84 NSString *hatFile = [[NSString alloc] initWithFormat:@"%@/%@", HATS_DIRECTORY(), hat]; |
85 UIImage *hatSprite = [[UIImage alloc] initWithContentsOfFile: hatFile andCutAt:CGRectMake(0, 0, 32, 32)]; |
85 UIImage *hatSprite = [[UIImage alloc] initWithContentsOfFile: hatFile andCutAt:CGRectMake(0, 0, 32, 32)]; |
86 [hatFile release]; |
86 [hatFile release]; |
87 cell.imageView.image = [self.normalHogSprite mergeWith:hatSprite atPoint:CGPointMake(0, 5)]; |
87 cell.imageView.image = [self.normalHogSprite mergeWith:hatSprite atPoint:CGPointMake(0, 5)]; |
88 [hatSprite release]; |
88 [hatSprite release]; |
89 |
89 |
90 if ([hat isEqualToString:[hog objectForKey:@"hat"]]) { |
90 if ([[hat stringByDeletingPathExtension] isEqualToString:[hog objectForKey:@"hat"]]) { |
91 cell.accessoryType = UITableViewCellAccessoryCheckmark; |
91 cell.accessoryType = UITableViewCellAccessoryCheckmark; |
92 self.lastIndexPath = indexPath; |
|
93 } else { |
92 } else { |
94 cell.accessoryType = UITableViewCellAccessoryNone; |
93 cell.accessoryType = UITableViewCellAccessoryNone; |
95 } |
94 } |
96 |
95 |
97 return cell; |
96 return cell; |
98 } |
97 } |
99 |
98 |
100 |
99 |
101 #pragma mark - |
100 #pragma mark - |
102 #pragma mark Table view delegate |
101 #pragma mark Table view delegate |
103 -(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
102 -(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath |
104 NSInteger newRow = [indexPath row]; |
103 { |
105 NSInteger oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1; |
104 NSInteger selectedRow = [indexPath row]; |
|
105 |
|
106 // update data on the hog dictionary |
|
107 NSDictionary *oldHog = [[self.teamDictionary objectForKey:@"hedgehogs"] objectAtIndex:self.selectedHog]; |
106 |
108 |
107 if (newRow != oldRow) { |
109 NSMutableDictionary *newHog = [[NSMutableDictionary alloc] initWithDictionary:oldHog]; |
108 // if the two selected rows differ update data on the hog dictionary and reload table content |
110 [newHog setObject:[[self.hatArray objectAtIndex:selectedRow] stringByDeletingPathExtension] forKey:@"hat"]; |
109 // TODO: maybe this section could be cleaned up |
111 [[self.teamDictionary objectForKey:@"hedgehogs"] replaceObjectAtIndex:self.selectedHog withObject:newHog]; |
110 NSDictionary *oldHog = [[teamDictionary objectForKey:@"hedgehogs"] objectAtIndex:selectedHog]; |
112 [newHog release]; |
111 |
113 |
112 NSMutableDictionary *newHog = [[NSMutableDictionary alloc] initWithDictionary: oldHog]; |
114 // tell our boss to write this new stuff on disk |
113 [newHog setObject:[[hatArray objectAtIndex:newRow] stringByDeletingPathExtension] forKey:@"hat"]; |
115 [[NSNotificationCenter defaultCenter] postNotificationName:@"setWriteNeedTeams" object:nil]; |
114 [[teamDictionary objectForKey:@"hedgehogs"] replaceObjectAtIndex:selectedHog withObject:newHog]; |
116 |
115 [newHog release]; |
|
116 |
|
117 // tell our boss to write this new stuff on disk |
|
118 [[NSNotificationCenter defaultCenter] postNotificationName:@"setWriteNeedTeams" object:nil]; |
|
119 |
|
120 UITableViewCell *newCell = [aTableView cellForRowAtIndexPath:indexPath]; |
|
121 newCell.accessoryType = UITableViewCellAccessoryCheckmark; |
|
122 UITableViewCell *oldCell = [aTableView cellForRowAtIndexPath:lastIndexPath]; |
|
123 oldCell.accessoryType = UITableViewCellAccessoryNone; |
|
124 self.lastIndexPath = indexPath; |
|
125 [aTableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone]; |
|
126 } |
|
127 [aTableView deselectRowAtIndexPath:indexPath animated:YES]; |
|
128 [self.navigationController popViewControllerAnimated:YES]; |
117 [self.navigationController popViewControllerAnimated:YES]; |
129 } |
118 } |
130 |
119 |
131 |
120 |
132 #pragma mark - |
121 #pragma mark - |
133 #pragma mark Memory management |
122 #pragma mark Memory management |
134 -(void) didReceiveMemoryWarning { |
123 -(void) didReceiveMemoryWarning { |
135 self.lastIndexPath = nil; |
|
136 MSG_MEMCLEAN(); |
124 MSG_MEMCLEAN(); |
137 [super didReceiveMemoryWarning]; |
125 [super didReceiveMemoryWarning]; |
138 } |
126 } |
139 |
127 |
140 -(void) viewDidUnload { |
128 -(void) viewDidUnload { |
141 self.lastIndexPath = nil; |
|
142 self.normalHogSprite = nil; |
129 self.normalHogSprite = nil; |
143 self.teamDictionary = nil; |
130 self.teamDictionary = nil; |
144 self.hatArray = nil; |
131 self.hatArray = nil; |
145 MSG_DIDUNLOAD(); |
132 MSG_DIDUNLOAD(); |
146 [super viewDidUnload]; |
133 [super viewDidUnload]; |