自己实现 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. Java 实现 WC.exe

    Github:https://github.com/YJOED/Code/tree/master/WC/src 一.题目:实现一个统计程序,它能正确统计程序文件中的字符数.单词数.行数,以及还具备其他 ...

  2. Android-AndroidStudio Run 'app'安装APK到设备的过程

    1.AndroidStudio 点击Run ‘app’. 2.点击Run ‘app’就会将所有.class文件用SDK工具集处理成.dex, 用SDK工具集将图片/资源/布局文件/AndroidMan ...

  3. 解决centOS 本地可以访问 外部主机不能访问的问题

    但是centos中的防火墙规则比较严密 用curl http://localhost:10000 可以看到内容,但是外部无法访问,原因是防火墙没有开启10000端口,需要将10000端口加入到信任规则 ...

  4. 工作中的Buff加成-结构化思考力:自创独门武功 3-3-3原则

    3-3-3原则 3秒钟 *思考三秒钟 沟通前根据交谈内容思考3秒钟,思考下具体需要表达什么,思考的主要点为What.Why.How. *冷静三秒钟 在沟通过程中,若遇到气氛不和谐,比如生气.愤恨等时, ...

  5. Python【运算符】

    本文介绍 1.Python运算符 运算符分类 运算符分为:算数运算.比较运算.逻辑运算.赋值运算.成员运算.身份运算.位运算 一.算数运算:返回数字 假设变量a=10,b=20 运算符: + 相加a+ ...

  6. Spring Cloud使用总结

    本文来自网易云社区,转载务必请注明出处. Spring Cloud 是spring团队推出的基于SpringBoot的分布式微服务框架,为开发者提供了在分布式系统(如配置管理.服务发现.断路器.智能路 ...

  7. 《Think in Java》20 21(并发)

    chapter 20 注解 三种标准注解和四种元注解: 编写注解处理器 chapter 21 并发 基本的线程机制 定义任务 package cn.test; public class LiftOff ...

  8. [ActionScript 3.0] 透视投影

    下面的示例演示如何使用透视投影来创建 3D 空间.该示例演示如何通过projectionCenter属性来修改消失点和更改空间的透视投影.进行这种修改后,将强制重新计算focalLength和fiel ...

  9. JSR133规范学习

    最近在看多线程相关的东西,通过阅读JSR133的faq来加深自己对多线程的理解,里面大部分的内容比较简单(越到后面越难),但是有的部分比较难以理解还没有完全弄懂,所以这里只记录了一下比较简单的阅读笔记 ...

  10. 【随记】SQL备份一张表的数据

    SQL Server: SELECT  *  INTO  table_bak   FROM   table Oracle.MySQL.SQLite: CREATE TABLE table_bak AS ...