这种方法早就发现了,不过一致没用,今天拿过来用,发现了一些问题。

1、这个东西和表视图结合使用很方便,首先,创建新的工程,将表视图控制器作为工程的根视图,并且添加一个导航(当然,你可以不这样做,但是你的搜索控制器要和表视图结合使用)

2、@interface TableViewController ()<UISearchControllerDelegate,UISearchResultsUpdating>,这里是要用的两个代理,

3、

@property(nonatomic,strong)NSArray *content;//在这里存放的是你的数据

@property(nonatomic,strong)NSArray *searchResult;//这个是搜索出来的结果

@property(nonatomic,strong)UISearchController *searchController;

4、在这里进行代理的设置以及一些属性(是叫属性吧)的初始化操作,,,,注意的是,搜索控制器的初始化放在代理的设置之前。

- (void)viewDidLoad {

[super viewDidLoad];

self.content = @[@"beijing",

@"shanghai",

@"guanghzou",

@"shenzhen",

@"huhuhu"

];

self.searchResult = @[];

self.searchController = [[UISearchController alloc]initWithSearchResultsController:nil];

self.searchController.searchResultsUpdater = self;

self.searchController.delegate = self;

[self.searchController.searchBar sizeToFit];

self.tableView.tableHeaderView = self.searchController.searchBar;

}

5、这里是代理方法的实现(只有这个是必须实现的,就是这个刷新了,这个方法的触发事件是什么)

#pragma mark UISearchResultsUpdating

-(void)updateSearchResultsForSearchController:(UISearchController *)searchController{

if (searchController.searchBar.text.length>0) {

self.searchResult = [self searchByText:searchController.searchBar.text];

}else{

self.searchResult = self.content;

}

[self.tableView reloadData];

}

//将搜索到的结果放到一个数组中返回,这里是搜索结果的判断

-(NSArray *)searchByText:(NSString *)text{

NSMutableArray *result = [NSMutableArray array];

for (NSString *str in self.content) {//遍历你存放所有数据的数组

if ([[str lowercaseString]rangeOfString:[text lowercaseString]].location != NSNotFound) {//这个方法头一回使用,这个跟NSPredicate有什么区别

[result addObject:str];

}

}

return result;

}

6、然后就是表视图的3问1答了

#pragma mark - Table view data source

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

return 1;

}

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

if (self.searchController.active) {//这里可以根据搜索控制器是不是激活状态来返回不同的数值,如果是搜索状态,表视图就返回搜索结果的个数个行,如果不在搜索的状态,就返回所有结果的个数

return self.searchResult.count;

}else{

return self.content.count;

}

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];

if (self.searchController.active) {//这里的逻辑同返回行数

cell.textLabel.text = self.searchResult[indexPath.row];

}else{

cell.textLabel.text = self.content[indexPath.row];

}

return cell;

}

另外,我该怎么实现对搜索结果的点击事件呢。

self.searchController.dimsBackgroundDuringPresentation = NO;

这样搜索的结果就可以点击了,(补充2015-9-22)

UISearchController的使用。(iOS8+)的更多相关文章

  1. iOS之搜索框UISearchController的使用(iOS8.0以后替代UISearchBar+display)

    在iOS 8.0以上版本中, 我们可以使用UISearchController来非常方便地在UITableView中添加搜索框. 而在之前版本中, 我们还是必须使用UISearchBar + UISe ...

  2. 搜索框UISearchController的使用(iOS8.0以后替代UISearchBar + UISearchDisplayController)

    1.searchResultsUpdater:设置显示搜索结果的控制器 ? 1     _mySearchController.searchResultsUpdater = self; 2.dimsB ...

  3. iOS --- 搜索框UISearchController的使用(iOS8.0以后替代UISearchBar+display)

    在iOS 8.0以上版本中, 我们可以使用UISearchController来非常方便地在UITableView中添加搜索框. 而在之前版本中, 我们还是必须使用UISearchBar + UISe ...

  4. UISearchController

    搜索框UISearchController的使用(iOS8.0以后替代UISearchBar + UIS) 1.在iOS 8.0以上版本中, 我们可以使用UISearchController来非常方便 ...

  5. iOS8以后 UISearchController的用法

    查了不少资料,都不太全,自己查看了apple文档,写了一份代码: 如下(只是界面): 1. 声明属性 @property (nonatomic, strong) UISearchController ...

  6. UISearchController使用

    iOS8之前我们使用UISearchDisplayController做TableView的本地搜索 iOS8提供实现搜索功能的SDK:UISearchController(iOS8.0之后).UIS ...

  7. iOS原生的搜索:UISearchController

    iOS8之前我们使用UISearchDisplayController做TableView的本地搜索,查看UIKit库,苹果已经使用新控件取代它. NS_CLASS_DEPRECATED_IOS(3_ ...

  8. iOS8的一些控件的变更---备用

    UISearchDisplayController变更为UISearchController UIAlertView变更为UIAlertController 如果添加点击事件则需要使用UIAlertC ...

  9. iOS UISearchBar UISearchController

    搜索栏的重要性我们就不说了,狼厂就是靠搜索起家的,现在越来越像一匹没有节操的狼,UC浏览器搜索栏现在默认自家的神马搜索,现在不管是社 交,O2O还是在线教育等都会有一个搜索栏的实现,不过彼此实现效果是 ...

随机推荐

  1. IE6完美解决fixed方法

    ie6对position:fixed不支持,网上有很多解决方法,有的在ie6,ie7上调试成功后,在ie8上又不好使,div层还是跟随滚动条浮 动:以下总结方法,在ie6,ie7,ie8上都调试成功, ...

  2. Oracle中的AS和IS

    Oracle中的AS和IS是ORACLE为了方便而设置的同义词基本上没有不同 . 使用规则: 1.在创建存储过程(PROCEDURE)/函数(FUNCTION),以及自定义类型(TPYE)和包(PAC ...

  3. Jfinal极速开发微信系列教程(一)--------------Jfinal_weixin demo的使用分析

    概述: Jfinal_weixin已经出了有好一段时间了!一直在关注当中......最近工作上有需要到这个东西,所以,话了两个小时来看看这个东西,看完demo以后,豁然开朗,原理微信和一般的web项目 ...

  4. Codeforces Codeforces Round #319 (Div. 2) C. Vasya and Petya's Game 数学

    C. Vasya and Petya's Game Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/ ...

  5. 【PAT Advanced Level】1006. Sign In and Sign Out (25)

    关键在于清空字符数组和使用scanf进行输入 #include <stdio.h> #include <string.h> #include <fstream> # ...

  6. Hive权限介绍

    一.开启权限 眼下hive支持简单的权限管理,默认情况下是不开启.这样全部的用户都具有同样的权限.同一时候也是超级管理员.也就对hive中的全部表都有查看和修改的权利,这样是不符合一般数据仓库的安全原 ...

  7. 应用之星推出“图文app”制作工具,并附上教程

    应用之星已推出的"图文"app制作工具,是高速制作图文电子书,图文杂志等一切有关图文资料的app生成工具,以下跟大家介绍"图文"制作教程,简单快捷,大致分三大步 ...

  8. Android模拟器操作快捷键

    你可以通过模拟器的启动选项和控制台命令来控制模拟环境的行为和特性.一旦模拟器启动,你就可以通过键盘和鼠标来“按” 模拟器的按键,从而操作模拟器.下面的表格总结了模拟器按键可键盘按键之间的映射关系. 模 ...

  9. Javascript call与apply记录

    [注]:记录自己对javascript中call与apply的见解 总会有些东西会被人拿出来重复的写来写去,为何? 只是因为自己感觉不够了解,所谓好记性不如烂笔头,并且在写的同时也会或多或少的收获到一 ...

  10. careercup-C和C++ 13.9

    13.9 编写支持对齐分配的malloc和free函数,分配内存时,malloc函数返回的地址必须都能被2的n次方整除. 解法: 一般来说,使用malloc,我们控制不了分配的内存会在堆里哪个位置.我 ...