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

最新文章

  1. NOI 1.5 42:画矩形
  2. NuGet在创建pack时提示”The replacement token &#39;author&#39; has no value“问题解决
  3. spark的安装
  4. Struts2(十五)实现文件上传
  5. python4delphi Python could not be properly initialized. We must quit.
  6. FOOD
  7. [iOS基础控件 - 6.9.4] 抓取网页图片资源
  8. java保留两位小数的方法
  9. AIX-df命令
  10. C/C++输入两个任意日期求相隔天数
  11. 查看Zookeeper服务器状态信息的一些命令
  12. C#的dapper使用
  13. Laravel 入口文件解读及生命周期
  14. CPU上下文切换
  15. jsp实现验证码登陆
  16. (Android数据传递)Service和Activity之间-- 借助BroadcastReceiver--的数据传递
  17. 纯CSS下拉菜单(希望对有需要的小伙伴有所帮助)
  18. (转)SqlDateTime 溢出。必须介于 1/1/1753 12:00:00 AM 和 12/31/9999 11:59:59 PM之间
  19. 【Python】批量爬取网站URL测试Struts2-045漏洞
  20. SocketIOCP

热门文章

  1. bea weblogic workshop中文乱码
  2. c#自定义控件属性面板及选择资源设置
  3. Spark提交任务到集群
  4. PHP导出excel文件
  5. jQuery编写的一款兼容IE6的图片轮播幻灯片
  6. mysql中character_set_connection的作用
  7. python Django 学习笔记(四)—— 使用MySQL数据库
  8. ios view的frame和bounds之区别(位置和大小)
  9. HTML5 API 之 history
  10. C# 将DataTable装换位List&lt;T&gt; 泛型