ios UITableView 搜索
自己实现 UITableView 搜索,相对于使用 UISearchDisplayController 来说自己写稍微麻烦了那么一点点,但是更加灵活。主要就是用一个字段区分出当前是搜索还是非搜索,然后 reload 相应的 data 就行了,和 UISearchDisplayController 的实现也很像,不过 UISearchDisplayController是两个 tableview 切换,这里我们是一个 tableview load 不同的数据。

关键代码:
@interface MainTableViewController : UIViewController<UISearchBarDelegate,UITableViewDelegate,UITableViewDataSource>{
UITableView *mytableView;
NSArray *data;
NSMutableArray *filterData;
BOOL isFiltered; // 标识是否正在搜素
UIView *mask;
}
- (void)viewDidLoad
{
[super viewDidLoad];
mytableView = [[UITableView alloc] initWithFrame:CGRectMake(, , self.view.frame.size.width, self.view.frame.size.height-)];
mytableView.dataSource = self;
mytableView.delegate = self;
[self.view addSubview:mytableView]; UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(, , self.view.frame.size.width
, )];
searchBar.placeholder = @"搜索";
searchBar.delegate = self;
mytableView.tableHeaderView = searchBar; // 添加一层 mask
mask = [[UIView alloc] initWithFrame:CGRectMake(, + , self.view.frame.size.width, self.view.frame.size.height - -)];
[self.view addSubview:mask];
mask.backgroundColor = [UIColor blackColor];
mask.alpha = ;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// 通过 isFiltered 区分出当前显示的是搜索结果集还是原结果集
if (isFiltered) {
return filterData.count;
} return data.count;
} - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellid = @"cellid";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellid]; if (cell==nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid];
} // 通过 isFiltered 区分出当前显示的是搜索结果集还是原结果集
if (isFiltered) {
cell.textLabel.text = filterData[indexPath.row];
}else{
cell.textLabel.text = data[indexPath.row];
} return cell;
} - (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar{
// 开始搜索时弹出 mask 并禁止 tableview 点击
NSLog(@"searchBarTextDidBeginEditing");
isFiltered = YES;
searchBar.showsCancelButton = YES;
mask.alpha = 0.3;
mytableView.allowsSelection = NO;
mytableView.scrollEnabled = NO;
} - (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar{
NSLog(@"searchBarTextDidEndEditing");
} - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
NSLog(@"textDidChange");
if (searchText.length == ) {
isFiltered = NO;
mask.alpha = 0.3;
mytableView.allowsSelection = NO;
mytableView.scrollEnabled = NO;
[mytableView reloadData];
return;
} isFiltered = YES;
mask.alpha = ;
mytableView.allowsSelection = YES;
mytableView.scrollEnabled = YES; // 谓词搜索
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self contains [cd] %@",searchText];
filterData = [[NSMutableArray alloc] initWithArray:[data filteredArrayUsingPredicate:predicate]];
[mytableView reloadData];
} - (void)searchBarCancelButtonClicked:(UISearchBar *) sb{
// 点击 cancel 时去掉 mask ,reloadData
sb.text = @"";
[sb setShowsCancelButton:NO animated:YES];
mytableView.allowsSelection = YES;
mytableView.scrollEnabled = YES;
[sb resignFirstResponder];
mask.alpha = ; isFiltered = NO;
[mytableView reloadData];
} -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *text; if (isFiltered) {
text = filterData[indexPath.row];
}else{
text = data[indexPath.row];
} NSLog(@"you click index:%d %@",indexPath.row,text);
}
ios UITableView 搜索的更多相关文章
- ios UISearchDisplayController 实现 UITableView 搜索功能
UISearchDisplayController 是苹果专为 UITableView 搜索封装的一个类. 里面内置了一个 UITableView 用于显示搜索的结果.它可以和一个需要搜索功能的 co ...
- IOS UITableView NSIndexPath属性讲解
IOS UITableView NSIndexPath属性讲解 查看UITableView的帮助文档我们会注意到UITableView有两个Delegate分别为:dataSource和deleg ...
- iOS UITableView划动删除的实现
标签:划动删除 iphone 滑动删除 ios UITableView 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://rainb ...
- iOS UITableView Tips(2)
#TableView Tips(2) (本来想一章就结束TableView Tips,但是发现自己还是太天真了~too young,too simple) ##架构上的优化 在Tips(1)中指出了一 ...
- iOS之搜索框UISearchController的使用(iOS8.0以后替代UISearchBar+display)
在iOS 8.0以上版本中, 我们可以使用UISearchController来非常方便地在UITableView中添加搜索框. 而在之前版本中, 我们还是必须使用UISearchBar + UISe ...
- iOS UITableView 与 UITableViewController
很多应用都会在界面中使用某种列表控件:用户可以选中.删除或重新排列列表中的项目.这些控件其实都是UITableView 对象,可以用来显示一组对象,例如,用户地址薄中的一组人名.项目地址. UITab ...
- iOS - UITableView中Cell重用机制导致Cell内容出错的解决办法
"UITableView" iOS开发中重量级的控件之一;在日常开发中我们大多数会选择自定Cell来满足自己开发中的需求, 但是有些时候Cell也是可以不自定义的(比如某一个简单的 ...
- 【转】iOS,搜索标签布局
前一阵时间,看过这样一个demo,代码不多,但是简洁易懂. 转自: // 代码地址: https://github.com/iphone5solo/PYSearch // 代码地址: http:/ ...
- iOS UITableView 引起的崩溃问题
其实 UITableView 应该是在iOS开发中使用最频繁的一个控件,一次同事之间聊天玩笑的说“一个页面,要是没使用UITableView,就好像称不上是一个页面”.虽然是个最常见的控件,但是他的强 ...
随机推荐
- PyCharm创建普通项目配置支持jinja2语法
打开项目的根目录的.idea文件夹中项目名.iml文件(隐藏文件) 打开这个iml文件,在component标签的同级,添加如下代码: <component name="Templat ...
- Git SSH Key
一.设置Git的user name和email: $ git config --global user.name "hhl_vip" $ git config --global ...
- BlangenOA项目展示(附源码)
1. 登录界面 1.1表单校验 1.2信息有误 1.3正在登录 2.桌面 3.用户管理 3.1添加 3.2删除 3.3编辑 3.4设置用户角色 3.5设置用户权限 4.角色管理 5.权限管理(菜单 ...
- 高德地图之c#后台获取一个或多个起点到单个终点的直线距离
首先我们需要一个控制台添加一个新Key(可使用服务选择Web服务,测试的时候IP白名单先不填); 直线距离是通过后台get方式请求API服务地址http://restapi.amap.com/v3/d ...
- 度度熊想去商场买一顶帽子,商场里有N顶帽子,有些帽子的价格可能相同。度度熊想买一顶价格第三便宜的帽子,问第三便宜的帽子价格是多少?
var data=[10,25,50,10,20,80,30,30,40,90]; function fun(arr,index){ var min=Math.min.apply(this,arr); ...
- Django Query
Making Qeries 一旦创建了数据模型,Django就会自动为您提供一个数据库抽象API,允许您创建.检索.更新和删除对象.本文档解释了如何使用这个API. The models 一个clas ...
- JAVA分解质因子
/*题目 分解质因数(5分) 题目内容: 每个非素数(合数)都可以写成几个素数(也可称为质数)相乘的形式,这几个素数就都叫做这个合数的质因数.比如,6可以被分解为2x3,而24可以被分解为2x2x2x ...
- 修改cmd默认启动路径
1.打开注册表编辑器(WIN+R打开运行.输入regedit,或者直接找到路径,双击打开C:\Windows\regedit.exe): 2.定位到“HKEY_CURRENT_USER\Softwar ...
- 卸载jdk1.7
卸载jdk1.7: 1.开始->程序->控制面板 ->卸载程序->程序和功能 2.找到jdk的两个程序:java 7 update 45和java(TM)SE Developm ...
- selenium爬取qq空间,requests爬取雪球网数据
一.爬取qq空间好友动态数据 # 爬取qq空间好友状态信息(说说,好友名称),并屏蔽广告 from selenium import webdriver from time import sleep f ...