码迷,mamicode.com
首页 > 其他好文 > 详细

search搜索

时间:2016-04-26 01:47:21      阅读:186      评论:0      收藏:0      [点我收藏+]

标签:

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate,UISearchBarDelegate,UISearchResultsUpdating>
@property(nonatomic,strong) NSMutableArray * datalist;
@property(nonatomic,strong) NSMutableArray * searchList;
@property(nonatomic,strong) UITableView * tableView;
@property (nonatomic, strong) UISearchController *searchController;
@end

@implementation ViewController
-(UITableView *)tableView {
    if (!_tableView) {
        _tableView=[[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
    }
   
    return _tableView;
   
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.datalist = [NSMutableArray arrayWithCapacity:100];
    for (NSUInteger i=0; i<100; i++) {
        [self.datalist addObject:[NSString stringWithFormat:@"%ld -FlyElephant",(long)i]];
    }
   
    _searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
    _searchController.searchResultsUpdater = self;
    _searchController.dimsBackgroundDuringPresentation = NO;
    _searchController.hidesNavigationBarDuringPresentation = NO;
    _searchController.searchBar.frame = CGRectMake(self.searchController.searchBar.frame.origin.x, self.searchController.searchBar.frame.origin.y, self.searchController.searchBar.frame.size.width, 44.0);
    self.tableView.tableHeaderView = self.searchController.searchBar;
    [self addTableView];
}
-(void)addTableView{
   
   
    self.tableView.delegate =self;
    self.tableView.dataSource =self;
    [self.view addSubview:self.tableView];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
   
    if (self.searchController.active) {
        return [self.searchList count];
    }else{
        return [self.datalist count];
    }
}
-(BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar{
   
   
    return YES;
}
-(BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar{
   
   
    NSLog(@"结束");
    return YES;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *flag=@"cellFlag";
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:flag];
    if (cell==nil) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:flag];
    }
    if (self.searchController.active) {
        [cell.textLabel setText:self.searchList[indexPath.row]];
    }
    else{
        [cell.textLabel setText:self.datalist[indexPath.row]];
    }
    return cell;
}
//- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString{
//    // 谓词的包含语法,之前文章介绍过http://www.cnblogs.com/xiaofeixiang/
//    NSPredicate *preicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[c] %@", searchString];
//    if (self.searchList!= nil) {
//        [self.searchList removeAllObjects];
//    }
//    //过滤数据
//    self.searchList= [NSMutableArray arrayWithArray:[_datalist filteredArrayUsingPredicate:preicate]];
//    //刷新表格
//    return YES;
//}
-(void)updateSearchResultsForSearchController:(UISearchController *)searchController {
    NSString *searchString = [self.searchController.searchBar text];
    NSPredicate *preicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[c] %@", searchString];
    if (self.searchList!= nil) {
        [self.searchList removeAllObjects];
    }
    //过滤数据
    self.searchList= [NSMutableArray arrayWithArray:[_datalist filteredArrayUsingPredicate:preicate]];
    //刷新表格
    [self.tableView reloadData];
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
   
    if (self.searchController.active) {
        NSLog(@"搜索行%ld",(long)indexPath.row);
    }else{
       
        NSLog(@"正常%ld",indexPath.row);
    }
   
   
}

search搜索

标签:

原文地址:http://www.cnblogs.com/hangman/p/5433358.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!