search搜索

@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 *[email protected]"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);
    }
   
   
}

时间: 2024-08-06 16:03:32

search搜索的相关文章

随笔之——各大热门网站search 搜索框的写法,浅析!

随笔之——各大热门网站search 搜索框的写法,浅析! 关于搜索框,写法有很多种,搜索框这一块是一个比较细的活,要先计算好他的高.宽: 下面我就以京东搜索框为例,给大家浅析一下. 上面就是最终search框效果图. 先送出代码>>>>>> 1 <span style="color: #ff00ff; font-size: 18pt;"><strong>HTML 标签:</strong></span>

【起航计划 032】2015 起航计划 Android APIDemo的魔鬼步伐 31 App-&gt;Search-&gt;Invoke Search 搜索功能 Search Dialog SearchView SearchRecentSuggestions

Search (搜索)是Android平台的一个核心功能之一,用户可以在手机搜索在线的或是本地的信息.Android平台为所有需要提供搜索或是查询功能的应用提 供了一个统一的Search Framework来帮助实现Search功能.Search Framework的UI可以有两种形式: 屏幕顶部的Search Dialog:如Google Map提供的搜索条. 可以嵌到ContentView中的SearchView,应用可以将它放在屏幕上任何地方. 不管采用那种UI,Android系统都可以通

django的前后的结合,search搜索功能案例

利用django的Q()功能可以很好的展开搜索功能 假设我要做个这样的搜索功能 那么思路是怎么样的? 前端获取 -->输入 -->ajax后台获取 -->进行搜索 -->将值返回后台 --->ajax在进行处理传到页面 那我们就来看看代码 前端的代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <ti

使用apt-cache search搜索想要的软件包

环境: Ubuntu14.10 我在编译u-boot代码的时候遇到了如下问题: LD test/dm/built-in.o CC examples/standalone/stubs.o LD examples/standalone/libstubs.o CC examples/standalone/hello_world.o LD examples/standalone/hello_world OBJCOPY examples/standalone/hello_world.srec OBJCOP

Thinkphp列表搜索排序-----查

一.控制器 // 列表 public function lst() { $model = D('Goods'); // 获取带翻页的数据 $data = $model->search(); $this->assign(array( 'data' => $data['data'], 'page' => $data['page'], )); $this->display(); } 二.模型中数据的查询 public function search() { /***********

Orchard搜索与索引

Orchard提供了索引与搜索的功能.开启Indexing属性可实现索引功能,伴随着一个特定的索引执行(默认包含基础搜索引擎).除了Indexing和Search提供查询索引的功能外(通过关键字或使用搜索引擎查询语法),还可以在前端返回与查询匹配的内容条目列表. 你必须开启Search.Indexing和Lucence这几个属性. 因为搜索基于索引,所以搜索自动开启了索引功能.当然要想使搜索与索引正常工作必须先开启搜索引擎. 当索引属性打开后,在设置下边会看到有Search(搜索)与Indexe

搜索引擎系列 ---lucene简介 创建索引和搜索初步

一.什么是Lucene? Lucene最初是由Doug Cutting开发的,2000年3月,发布第一个版本,是一个全文检索引擎的架构,提供了完整的查询引擎和索引引擎 :Lucene得名于Doug妻子的中名,同时这也她外祖母的姓;目前是Apache基金会的一个顶级项目,同时也是学习搜索引擎入门必知必会. Lucene 是一个 JAVA 搜索类库,它本身并不是一个完整的解决方案,需要额外的开发工作. 优点:成熟的解决方案,有很多的成功案例.apache 顶级项目,正在持续快速的进步.庞大而活跃的开

搜索引擎系列 -lucene简介 创建索引和搜索初步步骤

一.什么是Lucene? Lucene最初是由Doug Cutting开发的,2000年3月,发布第一个版本,是一个全文检索引擎的架构,提供了完整的查询引擎和索引引擎 :Lucene得名于Doug妻子的中名,同时这也她外祖母的姓;目前是Apache基金会的一个顶级项目,同时也是学习搜索引擎入门必知必会. Lucene 是一个 JAVA 搜索类库,它本身并不是一个完整的解决方案,需要额外的开发工作. 优点:成熟的解决方案,有很多的成功案例.apache 顶级项目,正在持续快速的进步.庞大而活跃的开

百度图像搜索探秘

源地址:http://blog.sina.com.cn/s/blog_6ae183910101gily.html 昨天,百度上线了新的相似图(similar image search)搜索,试了风景.人物.文字等不同类型query的效果,感觉效果非常赞.尤其对于人物搜索,返回的结果在颜色.以及姿态方面具有非常大的相似性.特别是在输入某个pose的美女图片时,会搜到一系列相近pose的美女图片,真的是宅男之福啊.本着娱乐精神,贴一个搜索结果供大家yy. 我们知道这个产品底层的技术是余凯老师领导的百