IOS中的高级控件表视图(为表视图加入搜索框)
在上面的一篇博客中我们已经实现的表视图控件
在前面的代码中,或者说基础上要相实现搜索框其实也非常简单,只要再加入一个控件
搜索框有两种,选择的时候我们要尽量选择下面一种因为下面那一种已经实现了该控件的一些方法
再加入SearchBar的协议,实现其的两个方法
协议:
UISearchBarDelegate
方法:-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
就可以简单的实现一个搜索框的效果
下面就贴出详细的代码
ViewController.h
@interface ViewController : 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
ViewController.m
@implementation ViewController
@synthesize allteams;
@synthesize teams;
@synthesize teamsname;
//重新搜索
-(void)resetSearch{
self.teams = self.allteams;
NSMutableArray *keyArray = [[NSMutableArray alloc] init];
[keyArray addObjectsFromArray:[[teams allKeys] sortedArrayUsingSelector:@selector(compare:)] ];
self.teamsname = keyArray;
[keyArray release];
}
//加载数据
- (void)viewDidLoad
{
[super viewDidLoad];
//下面为模式代码读取文件到代码中
NSBundle *bundle = [NSBundle mainBundle];
NSString *filePath = [bundle pathForResource:@"statedictionary" ofType:@"plist"];
NSMutableDictionary *dic = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath];
self.allteams = dic;
[dic release];
[self resetSearch];
}
//加载数据源
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
//返回段里面有几行
NSString *name = [teamsname objectAtIndex:section];
NSArray *team = [teams objectForKey:name];
return [team count];
}
//返回数量
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return [teamsname count];
}
//返回每个段里面的名字
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
NSString *name = [teamsname objectAtIndex:section];
return name;
}
//模式代码填充数据
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
NSUInteger section = [indexPath section];
NSUInteger row = [indexPath row];
NSString *name = [teamsname objectAtIndex:section];
NSArray*team = [teams objectForKey:name];
//返回协议的标题
cell.textLabel.text = [team objectAtIndex:row];
return cell;
}
//实现索引
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
return teamsname;
}
//实现表示图的方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSUInteger section = [indexPath section];
NSUInteger row = [indexPath row];
NSString *name = [teamsname objectAtIndex:section];
NSArray *team = [teams objectForKey:name];
NSString *selectedteam = [team objectAtIndex:row];
NSString *message = [[NSString alloc] initWithFormat:@"你选择的号码是%@",selectedteam];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"球队选择"
message:message delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil, nil];
[alert show];
[alert release];
[message release];
//实现点击时,让点击的那个选中慢慢消失
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
//查询的方法
-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{
[self resetSearch];
}
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
if([searchText length] == 0){
[self resetSearch];
return;
}
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
for(NSString *key in self.allteams){
NSMutableArray *arry = [allteams valueForKey:key];
NSMutableArray *newTeams = [[NSMutableArray alloc]init];
for(NSString *teamName in arry){
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)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)dealloc
{
[allteams release];
[teams release];
[teamsname release];
[super dealloc];
}
@end
最后看看效果图