UISearchController的使用。(iOS8+)

这种方法早就发现了,不过一致没用,今天拿过来用,发现了一些问题。数组

一、这个东西和表视图结合使用很方便,首先,建立新的工程,将表视图控制器做为工程的根视图,而且添加一个导航(固然,你能够不这样作,可是你的搜索控制器要和表视图结合使用)atom

二、@interface TableViewController ()<UISearchControllerDelegate,UISearchResultsUpdating>,这里是要用的两个代理,spa

三、代理

@property(nonatomic,strong)NSArray *content;//在这里存放的是你的数据code

@property(nonatomic,strong)NSArray *searchResult;//这个是搜索出来的结果事件

@property(nonatomic,strong)UISearchController *searchController;it

四、在这里进行代理的设置以及一些属性(是叫属性吧)的初始化操做,,,,注意的是,搜索控制器的初始化放在代理的设置以前。io

- (void)viewDidLoad {table

    [super viewDidLoad];class

    self.content = @[@"beijing",

                     @"shanghai",

                     @"guanghzou",

                     @"shenzhen",

                     @"huhuhu"

                     ];

        self.searchResult = @[];

      self.searchController = [[UISearchController alloc]initWithSearchResultsController:nil];

    self.searchController.searchResultsUpdater = self;

    self.searchController.delegate = self;

  [self.searchController.searchBar sizeToFit];

    self.tableView.tableHeaderView = self.searchController.searchBar;

 

}

 

五、这里是代理方法的实现(只有这个是必须实现的,就是这个刷新了,这个方法的触发事件是什么)

#pragma mark UISearchResultsUpdating

 

-(void)updateSearchResultsForSearchController:(UISearchController *)searchController{

    

    if (searchController.searchBar.text.length>0) {

        self.searchResult = [self searchByText:searchController.searchBar.text];

    }else{

        self.searchResult = self.content;

    }

    

    [self.tableView reloadData];

    

}

//将搜索到的结果放到一个数组中返回,这里是搜索结果的判断

-(NSArray *)searchByText:(NSString *)text{

    NSMutableArray *result = [NSMutableArray array];

    for (NSString *str in self.content) {//遍历你存放全部数据的数组

        if ([[str lowercaseString]rangeOfString:[text lowercaseString]].location != NSNotFound) {//这个方法头一回使用,这个跟NSPredicate有什么区别

            [result addObject:str];

        }

    }

    return result;

}

 六、而后就是表视图的3问1答了

#pragma mark - Table view data source

 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

 

   

    return 1;

}

 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

 

    if (self.searchController.active) {//这里能够根据搜索控制器是否是激活状态来返回不一样的数值,若是是搜索状态,表视图就返回搜索结果的个数个行,若是不在搜索的状态,就返回全部结果的个数

        return self.searchResult.count;

    }else{

        return self.content.count;

    }

}

 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];

    if (self.searchController.active) {//这里的逻辑同返回行数

         cell.textLabel.text = self.searchResult[indexPath.row];

     }else{

        cell.textLabel.text = self.content[indexPath.row];

    }

    

    

    return cell;

}

 

 

另外,我该怎么实现对搜索结果的点击事件呢。

 self.searchController.dimsBackgroundDuringPresentation = NO;

这样搜索的结果就能够点击了,(补充2015-9-22)

相关文章
相关标签/搜索