自己实现 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 搜索的更多相关文章

  1. ios UISearchDisplayController 实现 UITableView 搜索功能

    UISearchDisplayController 是苹果专为 UITableView 搜索封装的一个类. 里面内置了一个 UITableView 用于显示搜索的结果.它可以和一个需要搜索功能的 co ...

  2. IOS UITableView NSIndexPath属性讲解

    IOS UITableView NSIndexPath属性讲解   查看UITableView的帮助文档我们会注意到UITableView有两个Delegate分别为:dataSource和deleg ...

  3. iOS UITableView划动删除的实现

    标签:划动删除 iphone 滑动删除 ios UITableView 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://rainb ...

  4. iOS UITableView Tips(2)

    #TableView Tips(2) (本来想一章就结束TableView Tips,但是发现自己还是太天真了~too young,too simple) ##架构上的优化 在Tips(1)中指出了一 ...

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

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

  6. iOS UITableView 与 UITableViewController

    很多应用都会在界面中使用某种列表控件:用户可以选中.删除或重新排列列表中的项目.这些控件其实都是UITableView 对象,可以用来显示一组对象,例如,用户地址薄中的一组人名.项目地址. UITab ...

  7. iOS - UITableView中Cell重用机制导致Cell内容出错的解决办法

    "UITableView" iOS开发中重量级的控件之一;在日常开发中我们大多数会选择自定Cell来满足自己开发中的需求, 但是有些时候Cell也是可以不自定义的(比如某一个简单的 ...

  8. 【转】iOS,搜索标签布局

    前一阵时间,看过这样一个demo,代码不多,但是简洁易懂. 转自: //  代码地址: https://github.com/iphone5solo/PYSearch //  代码地址: http:/ ...

  9. iOS UITableView 引起的崩溃问题

    其实 UITableView 应该是在iOS开发中使用最频繁的一个控件,一次同事之间聊天玩笑的说“一个页面,要是没使用UITableView,就好像称不上是一个页面”.虽然是个最常见的控件,但是他的强 ...

随机推荐

  1. MlskincolorButton使用方法

    颜色设置 图标添加Png格式

  2. linux安装redis及phpredis环境配置

    下载安装包 cd /home/redis/tar wget http://redis.googlecode.com/files/redis-2.4.17.tar.gz tar zxvf redis-2 ...

  3. Mathcad操作tips:2D绘图

    1. 直接输入算式进行绘图(QuickPlot) 2. 先定义函数,再利用函数绘制多个曲线.一张图最多支持16条曲线.留意“,”的用法. 3. 利用空格键和","在现有绘图上增加新 ...

  4. docker容器中安装vi

    容器中输入vi提示 root@e36f8029c9f2:/# vi bash: vi: command not found 解决办法: 1.通过命令获取最新的软件包 apt-get update ap ...

  5. java 编译器级别与项目版本不匹配

    java compiler level does not match the v examime.      java 编译器级别与项目版本不匹配. 解决:      在当前项目上点右键,属性 Pro ...

  6. Windows上编译Boost

    Boost做得很好,有自己的build系统,可以几乎一键式编译,这才是尼玛世界一流质量的良心开源库啊. 将Boost 1.49.0解压到boost/boost_1_49_0里面,然后在boost目录底 ...

  7. MongoDB高级知识-易扩展

    MongoDB高级知识-易扩展 应用程序数据集的大小正在以不可思议的速度增长.随着可用宽带的增长和存储器价格的下跌,即使是一个小规模的应用程序,需要存储的数据也可能大的惊人,甚至超出了很多数据库的处理 ...

  8. 企业库连接形式简单例子记录 EnterpriseLibrary.Data

    结构图 webconfig 配置正确的连接字符串 <connectionStrings> <add name="DBConnection" connectionS ...

  9. Maven的安装环境变量配置

    针对新手刚接触maven,并且不知道如何去搭建和使用maven,那么我来写一篇浅显易懂的初级篇教程吧. 不同的是,别人会先将概念再安装,我来帮大家先搭建好以后再去看概念,不然概念会变的很模糊. 安装 ...

  10. ArrayList中的一些小细节@JDK8

    ArrayList中的一些小细节@JDK8 protected transient int modCount = 0; 该变量用于记录ArrayList的版本号,不可被序列化,每次对ArrayList ...