自己实现 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. [LintCode笔记了解一下]80.Median

    Given a unsorted array with integers, find the median of it. A median is the middle number of the ar ...

  2. Struts2的多方法动态Action

    原创 一个简单的Action里面的核心方法只有一个execute(); 定义多个核心方法的Action,有如下3种方式: 为Action配置method属性 动态方法调用 使用通配符映射方式 动态方法 ...

  3. ibatis 参数之模糊查询

    因项目需要最近使用ibatis,在使用查询语句的时候,想着通用性所以没有在配置文件里用N多的and 语句,而是如下: <select id="getUsersList" re ...

  4. TSQL--约束基础和Demo

    --============================================================ SQL SERVER 中使用constraint和role来对数据进行限制 ...

  5. RobotFramework做接口自动化(post请求)

    接口成功时返回: { "reCorde": "SUCCESS", "data": { ", "verify": ...

  6. html 线条重叠变粗

    加入属性 单元格重叠变粗 使用border-collapse:collapse;  可以解决.

  7. ML.NET Cookbook --- 1.如何从文本文件中加载数据?

    使用ML.NET中的TextLoader扩展方法从文本文件中加载数据.你需要知道在文本文件中数据列在那里,它们的类型是什么,在文本文件中什么位置可以找到它们. 请注意:对于ML.NET只读取文件的某些 ...

  8. 第一个.NET Core应用,创建.NET Core命令

    打开cmd,依次输入mkdir .project(创建目录),cd .\.project(进入目录),dotnet new(新建初始项目),dotnet restore(还原依赖),dotnet ru ...

  9. fatal: Authentication failed for又不弹出用户名和密码 解决办法

    各位,如果能弹出来,一定是你账号密码搞错了,就别继续看了.   image.png 切换命令行:   image.png 依然报错, 说到这个问题,又可以长篇大论了, 我使用的是tortoisegit ...

  10. 开发者都应该使用的10个C++11特性

    摘要: 在C++11新标准中,语言本身和标准库都增加了很多新内容,本文只涉及了一些皮毛.不过我相信这些新特性当中有一些,应该成为所有C++开发者的常规装备.你也许看到过许多类似介绍各种C++11特性的 ...