[转载地址]:http://blog.csdn.net/riveram/article/details/7340479
1.创建一个Navigation—based—Application项目,这样Interface Builder中会自动生成一个Table View,然后将Search Bar拖放到表示图上,以我们要给表示图添加搜索功能,不要忘记将Search Bar的delegate连接到File‘s Owner项,然后将Search Bar与searchBar变量连接。
2.在Resources文件夹下创建一个Movies.plist文件,然后为该文件添加一些数据,如下图:
3.在.h头文件添加如下内容:
- #import <UIKit/UIKit.h>
- @interface MyTableView : UITableViewController <UISearchBarDelegate>{
- NSDictionary *movieTitles;
- NSArray *years;
- IBOutlet UISearchBar *searchBar;
- BOOL isSearchOn;
- BOOL canSelectRow;
- //下面两个是搜索用到的两个变量
- NSMutableArray *listOfMovies;
- NSMutableArray *searchResult;
- }
- @property(nonatomic,retain) NSDictionary *movieTitles;
- @property(nonatomic,retain)NSArray *years;
- @property(nonatomic,retain)UISearchBar *searchBar;
- -(void)donSearching:(id)sender;
- -(void)searchMoviesTableView;
- @end
#import <UIKit/UIKit.h>
@interface MyTableView : UITableViewController <UISearchBarDelegate>{
NSDictionary *movieTitles;
NSArray *years;
IBOutlet UISearchBar *searchBar;
BOOL isSearchOn;
BOOL canSelectRow;
//下面两个是搜索用到的两个变量
NSMutableArray *listOfMovies;
NSMutableArray *searchResult;
}
@property(nonatomic,retain) NSDictionary *movieTitles;
@property(nonatomic,retain)NSArray *years;
@property(nonatomic,retain)UISearchBar *searchBar;
-(void)donSearching:(id)sender;
-(void)searchMoviesTableView;
@end
4.当加载View窗口时,首先定位属性列表并把这个列表加载到listOfMovies中,然后将所有的年份提取到years中,然后添加搜索条并初始化搜索条用到的数据:
- //读取Movies.plist文件的内容到变量里面
- - (void)viewDidLoad
- {
- NSString *path = [[NSBundle mainBundle]pathForResource:@"Movies" ofType:@"plist"];
- NSDictionary *dic = [[NSDictionary alloc]initWithContentsOfFile:path];
- self.movieTitles = dic;
- [dic release];
- NSArray *array = [[self.movieTitles allKeys]sortedArrayUsingSelector:@selector(compare:)];
- self.years = array;
- //下面两句是添加搜索条
- self.tableView.tableHeaderView = searchBar;
- self.searchBar.autocorrectionType = UITextAutocorrectionTypeYes;
- //初始化listofmovies
- listOfMovies = [[NSMutableArray alloc]init];
- for (NSString *year in years) {
- NSArray *movies = [movieTitles objectForKey:year];
- for(NSString *title in movies){
- [listOfMovies addObject:title];
- }
- }
- searchResult = [[NSMutableArray alloc]init];
- isSearchOn = NO;
- canSelectRow = YES;
- [super viewDidLoad];
- }
//读取Movies.plist文件的内容到变量里面
- (void)viewDidLoad
{
NSString *path = [[NSBundle mainBundle]pathForResource:@"Movies" ofType:@"plist"];
NSDictionary *dic = [[NSDictionary alloc]initWithContentsOfFile:path];
self.movieTitles = dic;
[dic release];
NSArray *array = [[self.movieTitles allKeys]sortedArrayUsingSelector:@selector(compare:)];
self.years = array;
//下面两句是添加搜索条
self.tableView.tableHeaderView = searchBar;
self.searchBar.autocorrectionType = UITextAutocorrectionTypeYes;
//初始化listofmovies
listOfMovies = [[NSMutableArray alloc]init];
for (NSString *year in years) {
NSArray *movies = [movieTitles objectForKey:year];
for(NSString *title in movies){
[listOfMovies addObject:title];
}
}
searchResult = [[NSMutableArray alloc]init];
isSearchOn = NO;
canSelectRow = YES;
[super viewDidLoad];
}
5.在自动生成的方法numberOfSectionsInTableView中添加如下代码,表示告诉表示图一共分多少节:
- - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
- {
- if (isSearchOn) {
- return 1;//如果正在搜索就只有一个section
- }
- else
- return [self.years count];
- }
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
- {
- if (isSearchOn) {
- return [searchResult count];
- }else{
- // Return the number of rows in the section.
- NSString *year = [self.years objectAtIndex:section];
- NSArray *movieSection = [self.movieTitles objectForKey:year];
- return [movieSection count];
- }
- }
- - (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];
- }
- if (isSearchOn) {
- NSString *cellValue = [searchResult objectAtIndex:indexPath.row];
- cell.textLabel.text = cellValue;
- }else{
- NSString *year = [self.years objectAtIndex:[indexPath section]];//得到当前行所在的section
- NSArray *movieSection = [self.movieTitles objectForKey:year];
- cell.textLabel.text = [movieSection objectAtIndex:[indexPath row]];
- cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
- }
- //为每一行添加图片
- UIImage *image = [UIImage imageNamed:@"apple.jpeg"];
- cell.imageView.image = image;
- return cell;
- }
- //设置每个section的标题
- -(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
- NSString *year = [self.years objectAtIndex:section];
- if (isSearchOn) {
- return nil;
- }
- else{
- return year;
- }
- }
- //添加索引
- -(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
- if (isSearchOn)
- return nil;
- else
- return years;
- }
- //搜索筐得到焦点后
- -(void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar{
- isSearchOn = YES;
- canSelectRow = NO;
- self.tableView.scrollEnabled = NO;
- //添加down按钮及其点击方法
- self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(donSearching:)]autorelease];
- }
- //点击down按钮后
- -(void)donSearching:(id)sender{
- isSearchOn = NO;
- canSelectRow = YES;
- self.tableView.scrollEnabled = YES;
- self.navigationItem.rightBarButtonItem = nil;
- [searchBar resignFirstResponder];
- [self.tableView reloadData];
- }
- //搜索筐里面的文字改变后
- -(void) searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
- if ([searchText length]>0) {
- isSearchOn = YES;
- canSelectRow = YES;
- self.tableView.scrollEnabled = YES;
- [self searchMoviesTableView];//调用搜索方法
- }
- else{
- isSearchOn = NO;
- canSelectRow = NO;
- self.tableView.scrollEnabled = NO;
- }
- [self.tableView reloadData];
- }
- //自定义的搜索方法,得到搜索结果
- -(void)searchMoviesTableView{
- [searchResult removeAllObjects];
- for (NSString *str in listOfMovies) {
- NSRange titleResultsRange = [str rangeOfString:searchBar.text options:NSCaseInsensitiveSearch];
- if (titleResultsRange.length > 0) {
- [searchResult addObject:str];
- }
- }
- }
- -(void) searchBarSearchButtonClicked:(UISearchBar *)searchBar{
- [self searchMoviesTableView];
- }
- //点击table某一行跳转页面
- - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
- {
- MyTableViewOneMessage *mytm = [[MyTableViewOneMessage alloc]initWithNibName:@"MyTableViewOneMessage" bundle:nil];
- NSString *year = [self.years objectAtIndex:[indexPath section]];
- NSArray *movieSection = [self.movieTitles objectForKey:year];
- NSString *movieTitle = [movieSection objectAtIndex:[indexPath row]];
- NSString *message = [[NSString alloc]initWithFormat:@"%@",movieTitle];
- mytm.message = message;
- [self.navigationController pushViewController:mytm animated:YES];
- [mytm release];
- }
Interface Builder中添加两个lable,具体的就不详细了,很简单的,下面是这个界面的.h和.m文件:
- #import <UIKit/UIKit.h>
- @interface MyTableViewOneMessage : UIViewController {
- IBOutlet UILabel *mylable;
- NSString *message;
- }
- @property(nonatomic,retain)UILabel *mylable;
- @property(nonatomic,retain)NSString *message;
- @end
- #import "MyTableViewOneMessage.h"
- @implementation MyTableViewOneMessage
- @synthesize mylable;
- @synthesize message;
- - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
- {
- self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
- if (self) {
- // Custom initialization
- }
- return self;
- }
- -(void)viewDidAppear:(BOOL)animated{
- self.mylable.text = message;
- }
- - (void)dealloc
- {
- [mylable release];
- [message release];
- [super dealloc];
- }
- - (void)didReceiveMemoryWarning
- {
- // Releases the view if it doesn't have a superview.
- [super didReceiveMemoryWarning];
- // Release any cached data, images, etc that aren't in use.
- }
- #pragma mark - View lifecycle
- - (void)viewDidLoad
- {
- self.navigationItem.title = @"Tableview传过来的值";
- [super viewDidLoad];
- // Do any additional setup after loading the view from its nib.
- }
- - (void)viewDidUnload
- {
- [super viewDidUnload];
- // Release any retained subviews of the main view.
- // e.g. self.myOutlet = nil;
- }
- - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
- {
- // Return YES for supported orientations
- return (interfaceOrientation == UIInterfaceOrientationPortrait);
- }
- @end
![](http://hi.csdn.net/attachment/201203/10/0_1331385352p0SR.gif)
![](http://hi.csdn.net/attachment/201203/10/0_1331385364L839.gif)
![](http://hi.csdn.net/attachment/201203/10/0_1331385378mEAZ.gif)
![](http://hi.csdn.net/attachment/201203/10/0_1331385406OGrR.gif)