// // YCViewController.h // 分组查询一 // // Created by 余超 on 14-5-19. // Copyright (c) 2014年 余超. All rights reserved. // #import <UIKit/UIKit.h> @interface YCViewController : UIViewController<UITableViewDataSource,UITableViewDelegate,UISearchBarDelegate>{ NSMutableDictionary *allTeams; NSMutableDictionary *teams; NSArray *teamsname; } @property(nonatomic,retain) NSMutableDictionary *allTeams; @property(nonatomic,retain) NSMutableDictionary *teams; @property(nonatomic,retain) NSArray *teamsname; -(void) resetSearch; @end
// // YCViewController.m // 分组查询一 // // Created by 余超 on 14-5-19. // Copyright (c) 2014年 余超. All rights reserved. // #import "YCViewController.h" @interface YCViewController () @end @implementation YCViewController @synthesize allTeams,teams,teamsname; #pragma -mark -------------用于处理searchBar委托的方法,用于加载所有的分组数据的显示--------------- -(void)resetSearch{ self.teams=self.allTeams; NSMutableArray *allKeys=[[NSMutableArray alloc]init]; [allKeys addObjectsFromArray:[[teams allKeys]sortedArrayUsingSelector:@selector(compare:)]]; self.teamsname=allKeys; [allKeys release]; } #pragma -mark -------------用于加载资源文件,用于视图的初始化的过程中--------------- - (void)viewDidLoad { [super viewDidLoad]; NSBundle *bundle=[NSBundle mainBundle]; NSData *data=[[NSData alloc]initWithContentsOfFile: [bundle pathForResource:@"2" ofType:@"jpg"]];//找到NSBundle的某一资源 UIImage *img=[UIImage imageWithData:data];//创建了可用的图像对象 [self.view setBackgroundColor:[UIColor colorWithPatternImage:img]];//UIColor colorWithPatternImage:方法是把图片转化为color类型 将背景换做提供的图片 NSString *plistPath = [bundle pathForResource:@"足球队dictionary" ofType:@"plist"]; NSMutableDictionary *dic=[[NSMutableDictionary alloc]initWithContentsOfFile:plistPath]; self.allTeams =dic; self.teamsname=[[dic allKeys]sortedArrayUsingSelector:@selector(compare:)]; [dic release]; [self resetSearch]; } #pragma -mark -------------用于UITableViewDataSource方法的实现--------------- #pragma -mark 显示每个分段下有几行 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ NSString *name = [teamsname objectAtIndex:section]; NSArray *sectionOfDatas=[[teams objectForKey:name]sortedArrayUsingSelector:@selector(compare:)]; return [sectionOfDatas count]; } // Row display. Implementers should *always* try to reuse cells by setting each cell‘s reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier: // Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls) #pragma -mark 显示某个索引下某列的某一行的显示 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ NSUInteger section=[indexPath section]; NSUInteger row=[indexPath row]; NSString *name = [teamsname objectAtIndex:section]; NSArray *sectionOfDatas=[[teams objectForKey:name]sortedArrayUsingSelector:@selector(compare:)]; static NSString *CellIdentify=@"CellIdentify"; UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:CellIdentify]; if (cell==nil) { cell=[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentify]autorelease]; } cell.textLabel.text=[sectionOfDatas objectAtIndex:row]; return cell; } #pragma -mark 显示总分段的个数 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView// Default is 1 if not implemented { return [teamsname count]; } #pragma -mark 显示每个分段的名称 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;// fixed font style. use custom view (UILabel) if you want something different { NSString *name = [teamsname objectAtIndex:section]; return name; } #pragma -mark 显示每个分段结束的标志 - (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{ return @""; } // Index - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView// return list of section titles to display in section index view (e.g. "ABCD...Z#") { return teamsname; } #pragma -mark -------------用于UITableViewDelegate方法的实现--------------- #pragma -mark 显示用户操作的选定索引的某一行值 // Called after the user changes the selection. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ NSUInteger section=[indexPath section]; NSUInteger row=[indexPath row]; NSString *name = [teamsname objectAtIndex:section]; NSArray *sectionOfDatas=[[teams objectForKey:name]sortedArrayUsingSelector:@selector(compare:)]; NSString *message=[[NSString alloc]initWithFormat:@"你选择了%@ %@队",name,[sectionOfDatas objectAtIndex:row]]; UIAlertView *view =[[UIAlertView alloc]initWithTitle:@"你最喜欢的队" message:message delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil]; [tableView deselectRowAtIndexPath:indexPath animated:YES]; [view show]; [view release]; [message release]; } #pragma -mark -------------用于SearchBarDelegate方法的实现--------------- #pragma -mark 通过组名来搜索这一组的情况 - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText // called when text changes (including clear) { if ([searchText isEqualToString:@""]) { [self resetSearch]; return; } NSMutableDictionary *dict=[[NSMutableDictionary alloc]init]; for(NSString *key in self.teams){ NSMutableArray *array=[allTeams objectForKey:key]; NSMutableArray *newTeams=[[NSMutableArray alloc]init]; for (NSString *teamname in array) { if ([teamname rangeOfString:searchText options:NSCaseInsensitiveSearch].location!=NSNotFound) { [newTeams addObject:teamname]; } } if ([newTeams count]>0) { [dict setObject:newTeams forKey:key]; } [newTeams release]; } self.teamsname=[[dict allKeys]sortedArrayUsingSelector:@selector(compare:)]; self.teams=dict; [dict release]; } -(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{ [self resetSearch]; } //------------------------------------------------------------------------------------------------------/// - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } #pragma -mark -------------用于处理属性的内存空间的释放处理--------------- -(void)viewDidUnload{ self.teamsname=nil; self.teams=nil; self.allTeams=nil; } - (void)dealloc { [teams release]; [teamsname release]; [allTeams release]; [super dealloc]; } @end