UIKit 框架之UISearchController

时间:2022-12-09 19:16:32
//
//  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=@["];
    _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);
    ) {
        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);
    ) {
        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 {

    ;
}

- (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

UIKit 框架之UISearchControllerUIKit 框架之UISearchControllerUIKit 框架之UISearchController