自己实现 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. 优化MYSQL配置文件MY.INI

    table_cache=1024 物理内存越大,设置就越大.默认为2402,调到512-1024最佳.由于每个客户端连接都会至少访问一个表,因此此参数的值与max_connections有关.当某一连 ...

  2. django 'set' object does not support indexing

    在定义Model之后使用syncdb 同步的时候报出这个错误,检查之后发现是用错了.在model的Meta里面,排序这些用的应该是 ordering = ['last','first','middle ...

  3. Nexys4 DDR MIG控制器引脚文件

    目前网上只有ucf文件,vivado不兼容ucf格式 使用c++字符串转换的xdc格式代码 set_property -dict { PACKAGE_PIN R7 IOSTANDARD SSTL18_ ...

  4. openedx使用中可能用到的一些资源

    这几天一直在弄openedx,你会发现安装好只是第一步,后面还有很多东西在等着你,那么哪里可以看到较新的资料了,分享几个站点: 1.https://readthedocs.org/projects/e ...

  5. sharepoint 2010 GetUserProfileByName 5566

    After some further investigation i found that it's actually the "ASP.NET Impersonation" th ...

  6. ASP.NET Core中使用自定义验证属性控制访问权限

    在应用中,有时我们需要对访问的客户端进行有效性验证,只有提供有效凭证(AccessToken)的终端应用能访问我们的受控站点(如WebAPI站点),此时我们可以通过验证属性的方法来解决. 一.publ ...

  7. [agc007f] Shik and Copying String 模拟神题

    Description ​ "全"在十分愉快打工,第0天,给了他一个仅有小写字母构成的长度为N的字符串S0,在之后的第i天里,"全"的工作是将Si−1复制一份到 ...

  8. while循环/格式化输出/ 逻辑运算/ 编码 /单位转换

    一.while 循环 1. 循环 while 条件: 代码块(循环体) else: 当上面的条件为假. 才会执行 执行顺序: 判断条件是否为真. 如果真. 执行循环体. 然后再次判断条件....直到循 ...

  9. JSON与XML比较

    1. 定义介绍 1.1 XML定义 扩展标记语言 (Extensible Markup Language, XML) ,用于标记电子文件使其具有结构性的标记语言,可以用来标记数据.定义数据类型,是一种 ...

  10. PHP将多维数组变成一维数组

    function reduceArray($array) { $return = []; array_walk_recursive($array, function ($x) use (&$r ...