//ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
<UITableViewDelegate,UITableViewDataSource,UISearchDisplayDelegate>
{
NSMutableArray *nameArray;
UISearchBar *searchBar;
NSMutableArray *searchResult;
IBOutlet UITableView *myTableView;
}
@end
//ViewController.m
#import "ViewController.h"
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(10, 0, 300, 44)];
searchResult = [[NSMutableArray alloc]initWithCapacity:0];
myTableView.tableHeaderView = searchBar;
UISearchDisplayController *display = [[UISearchDisplayController alloc]initWithSearchBar:searchBar contentsController:self];
// display.delegate = self;
display.searchResultsDataSource = self;
display.searchResultsDelegate = self;
nameArray = [[NSMutableArray alloc] initWithCapacity:0];
for (int i = 'A'; i<='Z'; i++) {
NSMutableArray *subArray = [[NSMutableArray alloc]initWithCapacity:0];
for (int j=0; j<5; j++) {
NSString *name = [NSString stringWithFormat:@"%c %d",i,j];
[subArray addObject:name];
}
[nameArray addObject:subArray];
}
}
- (NSInteger)numberOfSectionsInTableView:(UITableView*)tableView
{
if (tableView!=myTableView) {
return 1;
}
return [nameArray count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView != myTableView) {
//搜索开始
//清空上次搜索结果
[searchResult removeAllObjects];
NSString *searchWord = searchBar.text;
for (NSArray *temp in nameArray) {
for (NSString *str in temp) {
NSRange range = [strrangeOfString:searchWord options:NSCaseInsensitiveSearch];
if (range.location == NSNotFound) {
continue;
}
[searchResult addObject:str];
}
}
return [searchResult count];
}
return [[nameArray objectAtIndex:section] count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:@"myCell"];
if (cell == nil) {
cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:@"myCell"] autorelease];
}
if (tableView != myTableView) {
cell.textLabel.text =[searchResultobjectAtIndex:indexPath.row];
return cell;
}
cell.textLabel.text = [[nameArrayobjectAtIndex:indexPath.section]objectAtIndex:indexPath.row];
return cell;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
if (tableView!=myTableView) {
return @"搜索结果";
}
return [NSString stringWithFormat:@"%c",section+'A'];
}
- (NSArray *)sectionIndexTitlesForTableView:(UITableView*)tableView
{
if (tableView == myTableView) {
NSMutableArray *charArray = [[NSMutableArray alloc]initWithCapacity:0];
//加一个搜索索引
[charArray addObject:UITableViewIndexSearch];
for (int i='A'; i<'Z'; i++) {
NSString *name = [NSStringstringWithFormat:@"%c",i];
[charArray addObject:name];
}
return charArray;
}
return nil;
}
//加入搜索图标后 (搜索位置与段落错位)
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
if (index == 0) {
[tableViewscrollRectToVisible:tableView.tableHeaderView.frameanimated:YES];
}
return index-1;
}
- (void)dealloc {
[myTableView release];
[super dealloc];
}
@end