UITableView的搜索功能8.0以前版本

/*

1.创建一个导航栏

2.写一个数据源

3.创建搜索栏

a.让tableView的表头等于搜索栏

b.把搜索栏控制器用搜索栏初始化

self.sdc = [[UISearchDisplayController alloc] initWithSearchBar:self.searchBar contentsController:self];

c.搜索栏继承两个协议

    self.sdc.searchResultsDataSource = self;
    self.sdc.searchResultsDelegate = self;

 */

#import "MyTableViewController.h"

@interface MyTableViewController ()
//数据源
@property (nonatomic) NSMutableArray *dataArr;
//搜索栏
@property (nonatomic) UISearchBar *searchBar;
//搜索显示控制器
@property (nonatomic) UISearchDisplayController *sdc;

//定义搜索结果数组
@property (nonatomic) NSMutableArray *searchResult;
//段头的数据
@property (nonatomic) NSMutableArray *sectionHeadArr;

@end

@implementation MyTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self customNaviItem];
    [self createDataSource];
    [self createSearchBar];
}

#pragma mark    ------------------------定制导航栏目
- (void)customNaviItem
{
    //标题
    self.navigationItem.title = @"联系人";
}

#pragma mark    ------------------------创建数据源
- (void)createDataSource
{
    self.dataArr = [[NSMutableArray alloc] init];
    for(int i='A';i<='Z';i++)
    {
        NSMutableArray *arr = [[NSMutableArray alloc] init];
        for(int j=0;j<10;j++)
        {
            NSString *str = [NSString stringWithFormat:@"%c%c%c%c",i,arc4random()%26+'A',arc4random()%26+'A',arc4random()%26+'A'];
            [arr addObject:str];//一个数组,相当于一个段
        }
        [self.dataArr addObject:arr];
    }
    //段头的数据
    self.sectionHeadArr = [[NSMutableArray alloc] init];//每个段的段头A---Z
    for(int k='A';k<='Z';k++)
    {
        NSString *str = [NSString stringWithFormat:@"%c",k];
        [self.sectionHeadArr addObject:str];
    }
}

#pragma mark    ------------------------创建搜索栏目
- (void)createSearchBar
{
    self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 40)];
    self.tableView.tableHeaderView = self.searchBar;
    
    self.searchBar.autocapitalizationType = UITextAutocapitalizationTypeAllCharacters;
    self.sdc = [[UISearchDisplayController alloc] initWithSearchBar:self.searchBar contentsController:self];
    //设置数据源代理
    self.sdc.searchResultsDataSource = self;
    self.sdc.searchResultsDelegate = self;
}

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    // Return the number of sections.
    if (self.tableView==tableView) {
        return self.dataArr.count;
    }else{
        return 1;
    }
}

//返回段中的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    if (self.tableView == tableView) {
        return [self.dataArr[section] count];
    }
    
    if(self.searchResult == nil){
        self.searchResult = [[NSMutableArray alloc] init];
    }else{
        //清除上一次搜索结果
        [self.searchResult removeAllObjects];
    }
    for(NSArray *arr in self.dataArr){
        for(NSString *name in arr){
            //AABB  A
            NSRange rang = [name rangeOfString:self.searchBar.text];
            if (rang.location != NSNotFound) {
                [self.searchResult addObject:name];
            }
        }
    }
    return self.searchResult.count;
}

//给tableviewCell赋值
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    if(cell == nil){
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
    }
    if(self.tableView == tableView){
        cell.textLabel.text = self.dataArr[indexPath.section][indexPath.row];
    }else{
        cell.textLabel.text = self.searchResult[indexPath.row];
    }
    return cell;
}

#pragma mark    ------------------------改变段头高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    //设置段头高
    return 40;
}

#pragma mark    ------------------------显示段头文字
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if (self.tableView == tableView) {
        return self.sectionHeadArr[section];
    }else{
        return @"";
    }
}

#pragma mark    ------------------------显示索引
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    //注:索引和段保持一致
    return self.sectionHeadArr;
}

#pragma mark    ------------------------让索引没有用
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
    if(index == 2){
        return -1;
    }
    if([title isEqualToString:@"I"]){
        return -1;
    }
    return index;
}
@end

posted @ 2015-09-28 19:33  阿凡提王  阅读(97)  评论(0编辑  收藏  举报