001.#import "SectionsViewController.h"
002.#import "NSDictionary-MutableDeepCopy.h"
003.
004.@implementation SectionsViewController
005.@synthesize names;
006.@synthesize keys;
007.@synthesize table;
008.@synthesize search;
009.@synthesize allNames;
010.
011.#pragma mark -
012.#pragma mark Custom Methods
013.
014.
015.-(void)resetSearch
016.{
017. self.names = [self.allNames mutableDeepCopy];
018.
019.
020. NSMutableArray *keyArray = [[NSMutableArray alloc] init];
021.
022.
023. [keyArray addObject:UITableViewIndexSearch];
024.
025. [keyArray addObjectsFromArray:[[self.allNames allKeys] sortedArrayUsingSelector:@selector(compare:)]];
026. self.keys = keyArray;
027. [keyArray release];
028.
029.}
030.
031.
032.
033.
034.-(void)handleSearchForTerm:(NSString *)searchTerm
035.{
036.
037. NSMutableArray *selectionsToRemove = [[NSMutableArray alloc] init];
038. [self resetSearch];
039.
040.
041. for(NSString *key in self.keys)
042. {
043.
044. NSMutableArray *array = [names valueForKey:key];
045.
046. NSMutableArray *toRemove = [[NSMutableArray alloc] init];
047.
048.
049. for(NSString *name in array)
050. {
051.
052.
053.
054.
055.
056. if([name rangeOfString:searchTerm options:NSCaseInsensitiveSearch].location == NSNotFound)
057. [toRemove addObject:name];
058. }
059.
060.
061. if([array count] == [toRemove count])
062. [selectionsToRemove addObject:key];
063.
064.
065. [array removeObjectsInArray:toRemove];
066. [toRemove release];
067. }
068.
069.
070. [self.keys removeObjectsInArray:selectionsToRemove];
071. [selectionsToRemove release];
072. [table reloadData];
073.
074.}
075.
076.
077.- (void)viewDidLoad {
078.
079. NSString *path = [[NSBundle mainBundle] pathForResource:@"sortednames" ofType:@"plist"];
080.
081. NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path];
082. self.allNames = dict;
083. [dict release];
084.
085. [self resetSearch];
086.
087.
088.
089.
090.
091. [table reloadData];
092.
093. [table setContentOffset:CGPointMake(0.0f, 44.0f) animated:NO];
094.
095. [super viewDidLoad];
096.}
097.
098.- (void)didReceiveMemoryWarning {
099.
100. [super didReceiveMemoryWarning];
101.
102.
103.}
104.
105.- (void)viewDidUnload {
106.
107.
108.
109. self.table = nil;
110. self.search = nil;
111. self.allNames = nil;
112. self.names = nil;
113. self.keys = nil;
114.
115.}
116.
117.
118.- (void)dealloc {
119. [table release];
120. [search release];
121. [allNames release];
122. [keys release];
123. [names release];
124. [super dealloc];
125.}
126.
127.#pragma mark -
128.#pragma mark Table View Data Source Methods
129.-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
130.{
131.
132.
133. return ([keys count] > 0) ? [keys count] : 1;
134.}
135.
136.-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
137.{
138. if([keys count] == 0)
139. return 0;
140.
141. NSString *key = [keys objectAtIndex:section];
142. NSArray *nameSection = [names objectForKey:key];
143.
144. return [nameSection count];
145.
146.}
147.
148.-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
149.{
150. NSUInteger section = [indexPath section];
151. NSUInteger row = [indexPath row];
152.
153. NSString *key = [keys objectAtIndex:section];
154. NSArray *nameSection = [names objectForKey:key];
155.
156. static NSString *SectionTableIdentifier = @"SectionTableIdentifier";
157.
158. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SectionTableIdentifier];
159.
160. if(cell == nil)
161. cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SectionTableIdentifier] autorelease];
162.
163. cell.textLabel.text = [nameSection objectAtIndex:row];
164. return cell;
165.
166.}
167.
168.
169.-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
170.{
171. if([keys count] == 0)
172. return nil;
173.
174. NSString *key = [keys objectAtIndex:section];
175.
176.
177. if(key == UITableViewIndexSearch)
178. return nil;
179.
180. return key;
181.}
182.
183.
184.-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
185.{
186.
187. if(isSearching)
188. return nil;
189.
190. return keys;
191.}
192.
193.#pragma mark -
194.#pragma mark Table View Delegate Methods
195.
196.-(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
197.{
198.
199. [search resignFirstResponder];
200. isSearching = NO;
201. search.text = @"";
202. [tableView reloadData];
203. return indexPath;
204.}
205.
206.#pragma mark -
207.#pragma mark Search Bar Delegate Methods
208.
209.
210.-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
211.{
212. NSString *searchTerm = [searchBar text];
213. [self handleSearchForTerm:searchTerm];
214.}
215.
216.
217.-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchTerm
218.{
219.
220. if([searchTerm length] == 0)
221. {
222. [self resetSearch];
223. [table reloadData];
224. return;
225. }
226.
227. [self handleSearchForTerm:searchTerm];
228.
229.}
230.
231.
232.-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
233.{
234.
235. isSearching = NO;
236.
237. search.text = @"";
238. [self resetSearch];
239. [table reloadData];
240. [searchBar resignFirstResponder];
241.}
242.
243.
244.-(void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
245.{
246. isSearching = YES;
247. [table reloadData];
248.}
249.
250.
251.-(NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
252.{
253. NSString *key = [keys objectAtIndex:index];
254.
255. if(key == UITableViewIndexSearch)
256. {
257.
258. [tableView setContentOffset:CGPointZero animated:NO];
259. return NSNotFound;
260. } else
261. return index;
262.}
263.
264.@end