UIKit 框架之UISearchController

简介:

//
//  tableViewController.m
//  searchController
//
//  Created by City--Online on 15/6/1.
//  Copyright (c) 2015年 CYW. All rights reserved.
//

#import "tableViewController.h"

@interface tableViewController ()<UISearchResultsUpdating,UISearchControllerDelegate>
@property(nonatomic,strong) NSArray *allData;
@property(nonatomic,strong) NSMutableArray *searchData;
@end

@implementation tableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _allData=@[@"123",@"124",@"234",@"256",@"678",@"786",@"12"];
    _searchData=[_allData mutableCopy];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(search )];
    self.tableView.tableFooterView=[[UIView alloc]initWithFrame:CGRectZero];
   
}
-(void)search
{
//    为nil时是其本身
    UISearchController *searchvc=[[UISearchController alloc]initWithSearchResultsController:nil];
    searchvc.searchBar.tintColor=[UIColor orangeColor];
    searchvc.searchBar.barTintColor=[UIColor redColor];
    searchvc.definesPresentationContext=YES;
    searchvc.hidesNavigationBarDuringPresentation=NO;
    [searchvc.searchBar sizeToFit];
    searchvc.searchResultsUpdater=self;
    searchvc.delegate=self;
    [self presentViewController:searchvc animated:YES completion:nil];
}

//搜索栏文本内容改变时触发 和下面的
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController
{
    NSLog(@"aaa%@",searchController.searchBar.text);
    if (searchController.searchBar.text.length!=0) {
        NSPredicate *predicate=[NSPredicate predicateWithFormat:@"SELF CONTAINS %@",searchController.searchBar.text];
        _searchData=[[_allData filteredArrayUsingPredicate:predicate] copy];
    }
    [self.tableView reloadData];
    
}
//搜索视图将要消失时触发
-(void)willDismissSearchController:(UISearchController *)searchController
{
    
    NSLog(@"%@",searchController.searchBar.text);
    if (searchController.searchBar.text.length!=0) {
        NSPredicate *predicate=[NSPredicate predicateWithFormat:@"SELF CONTAINS %@",searchController.searchBar.text];
        _searchData=[[_allData filteredArrayUsingPredicate:predicate] copy];
    }
    else
    {
         _searchData=[_allData mutableCopy];
    }
    [self.tableView reloadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

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

    return 1;
}

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

    return _searchData.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
    cell.textLabel.text=[_searchData objectAtIndex:indexPath.row];
    
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"%@",indexPath);
}



@end




相关文章
|
Android开发 iOS开发