iOS 搜索条使用详解
在ios开发中搜索条的使用挺常见的,不过之前一直没用到也没细细研究,最近做外包项目的时候刚好用到,在这里记录一下使用的过程,只要理解了原理,其实还是比较简单的!上传的图片有点大,刚好可以看清楚它的使用效果!
我喜欢一步步的解析控件使用过程,其实真正的用心做一件事情是很享受的,虽然现在的社会都很注重效率和回报,这也是中国这种社会环境下难出大师级的人物的很重要的一个因素。扯得有点远了,只是希望国内开发者不要太急功近利,熟练应用本不是难事,一点点积累就好。下面开始搜索条
UISearchBar 和 UISearchDisplayController 的使用介绍。
第一步:添加UISearchBarDelegate,UISearchDisplayDelegate 委托
第二步:就是UISearchBar 和UISearchDisplayController的使用
//搜索框
self.searchBar = [[[UISearchBar alloc] initWithFrame:CGRectMake(10, 15, self.view.bounds.size.width-20, 45)]autorelease];
searchBar.placeholder=@"输入城市名或拼音";
searchBar.delegate = self;
searchBar.backgroundColor=[UIColor clearColor];
searchBar.backgroundImage=[UIImage imageNamed:@"搜索背景"];
searchBar.keyboardType=UIKeyboardTypeDefault;
myTableView.tableHeaderView = searchBar;
searchBar.autocorrectionType = UITextAutocorrectionTypeNo;
searchBar.autocapitalizationType = UITextAutocapitalizationTypeNone;
self.searchdispalyCtrl = [[[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self]autorelease];
//设置取消按钮文字颜色
searchdispalyCtrl.searchBar.tintColor=BB_Yanzheng_Color;
//search dispalyCtrl.active = NO;
//[searchdispalyCtrl.searchResultsTableView setHidden:NO];
searchdispalyCtrl.delegate = self;
searchdispalyCtrl.searchResultsDelegate=self;
searchdispalyCtrl.searchResultsDataSource = self;
这里面有两个点需要注意一下,一个是 myTableView.tableHeaderView = searchBar;这里是将searchbar放在对应列表的第一个section之中,而且section的高度不用设置,当然可以通过addsubview添加这种方式会产生一种移位,建议默认的方式比较好些!另一点是UISearchDisplayController是对searchbar的进一步封装,里面的东西确实简化了好多,可以采用这种方法设置取消按钮文本的颜色searchdispalyCtrl.searchBar.tintColor=BB_Yanzheng_Color;
第三步:相关委托的使用
我们通过实验发现点击searchbar的输入框会弹出一个独立的tableview视图,并显示一个取消按钮,搜索的数据显示在这个独立的视图列表中。而这个视图列表和普通的列表用法一样,需要设置
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
if (tableView == self.searchDisplayController.searchResultsTableView)
{
return 1;
}
else
{
return 26;
}
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView == self.searchDisplayController.searchResultsTableView)
{
NSLog(@"我去:%@",searchResult);
return [searchResult count];
}
else
{
if (section==0||section==1||section==2||section==3)
{
return 1;
}
if (isZiMu)
{
NSLog(@"zimugeshu:%@,%d",[zimuList objectAtIndex:section-4],[[cityGroupList objectForKey:[zimuList objectAtIndex:section-4]]count]);
return [[cityGroupList objectForKey:[zimuList objectAtIndex:section-4]]count];
}
/*
char mychar='a';
//当前字母
NSString *zimu=[NSString stringWithFormat:@"%c",mychar-32];
//NSString *zimu=[NSString stringWithFormat:@"%c",c];
if (![zimu isEqualToString:@"i"]&&![zimu isEqualToString:@"o"]&&![zimu isEqualToString:@"u"]&&![zimu isEqualToString:@"v"])
{
NSLog(@"zimugeshu:%@,%d",zimu,[[cityGroupList objectForKey:zimu]count]);
return [[cityGroupList objectForKey:[zimuList objectAtIndex:section-4]]count];
}*/
return 0;
}
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableView == self.searchDisplayController.searchResultsTableView)
{
return 45;
}
if (indexPath.section==0)
{
return 0;
}
if (indexPath.section==1||indexPath.section==2)
{
return 73;
}
if (indexPath.section==3)
{
//超过的话固定显示203
if (17+([remenCityList count]/3+1)*47+18<=223)
{
if ([remenCityList count]!=0&&[remenCityList count]%3==0)
{
return 17+([remenCityList count]/3)*47;
}
return 17+([remenCityList count]/3+1)*47;
}
return 223;
}
return 45;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
if (tableView == self.searchDisplayController.searchResultsTableView)
{
return 0;
}
if (section!=0)
{
return 32;
}
return 0.01f;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
if (tableView == self.searchDisplayController.searchResultsTableView)
{
return 0.01;
}
return 0.01f;
}
这些个基本的高度和行数 模块信息,当然列表的显示更是必不可少
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableView == self.searchDisplayController.searchResultsTableView)
{
static NSString *simpleTableIdentifier = @"RecipeCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier]autorelease];
}
else
{
for(UIView *view in cell.contentView.subviews)
{
[view removeFromSuperview];
}
}
//cell.textLabel.text = [searchResult objectAtIndex:indexPath.row];
//[cell.textLabel setTextColor:BB_Red_Color];
//NSLog(@"zheli:%@,%d,%@",[searchResult objectAtIndex:indexPath.row],indexPath.row,self.searchDisplayController.searchResultsTableView);
//城市名称
UILabel *labLeft = [[[UILabel alloc]init]autorelease];
labLeft.backgroundColor = [UIColor clearColor];
labLeft.textAlignment = NSTextAlignmentLeft;
labLeft.font = [UIFont systemFontOfSize:15];
labLeft.textColor = [UIColor blackColor];
labLeft.frame = CGRectMake(15, 15, 200, 15);
labLeft.text=[searchResult objectAtIndex:indexPath.row];
[cell.contentView addSubview:labLeft];
return cell;
}
}
上面列出的只是显示信息,真正数据的搜索要依赖于下面的委托方法实现
#pragma mark - UISearchDisplayDelegate
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
[self filterContentForSearchText:searchString
scope:[self.searchBar scopeButtonTitles][self.searchBar.selectedScopeButtonIndex]];
NSLog(@"woshuoba:%@,%@,%d,%@",searchString,self.searchBar.scopeButtonTitles,self.searchBar.selectedScopeButtonIndex,[self.searchBar scopeButtonTitles][self.searchBar.selectedScopeButtonIndex]);
return YES;
}
-(void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
NSMutableArray *tempResults = [[[NSMutableArray array]init]autorelease];
NSMutableArray *tempResultsId = [[[NSMutableArray array]init]autorelease];
[tempResults removeAllObjects];
[tempResultsId removeAllObjects];
NSUInteger searchOptions = NSCaseInsensitiveSearch | NSDiacriticInsensitiveSearch;
NSLog(@"sousuo:%@,%@,%@",searchText,scope,storeResults);
for (int i = 0; i < storeResults.count; i++)
{
NSString *storeString = [[storeResults objectAtIndex:i]objectForKey:@"name"];
NSRange storeRange = NSMakeRange(0, storeString.length);
NSRange foundRange = [storeString rangeOfString:searchText options:searchOptions range:storeRange];
if (foundRange.length)
{
NSLog(@"foundRange:%d,%d",foundRange.length,foundRange.location);
[tempResults addObject:storeString];
[tempResultsId addObject:[[storeResults objectAtIndex:i]objectForKey:@"id"]];
}
NSLog(@"wodejieguo:%@,%@",[[storeResults objectAtIndex:i]objectForKey:@"id"],storeString);
}
//清空搜索内容Id
[self.searchResultId removeAllObjects];
[self.searchResult removeAllObjects];
[self.searchResultId addObjectsFromArray:tempResultsId];
[self.searchResult addObjectsFromArray:tempResults];
NSLog(@"searchResult:%@,%@",searchResult,searchResultId);
}
说道这里关于搜索条的使用已经基本完成,当然搜索列表选中跳页可以借助自定义委托来实现,这里就不多少了!
iOS 搜索条使用详解的更多相关文章
- ios新特征 ARC详解
IOS ARC 分类: IOS ARC2013-01-17 09:16 2069人阅读 评论(0) 收藏 举报 目录(?)[+] 关闭工程的ARC(Automatic Reference Co ...
- IOS—UITextFiled控件详解
IOS—UITextFiled控件详解 //初始化textfield并设置位置及大小 UITextField *text = [[UITextField alloc]initWithFrame:CGR ...
- IOS 友盟使用详解
IOS 友盟使用详解 这篇博客将会详细介绍友盟的使用,希望对博友们有所帮助. 首先我们在浏览器上搜索友盟. 在这里我们选择官网这个,进去友盟官网后我们按照下图进行选择. 接下来选择如下图 Next 这 ...
- Lucene系列六:Lucene搜索详解(Lucene搜索流程详解、搜索核心API详解、基本查询详解、QueryParser详解)
一.搜索流程详解 1. 先看一下Lucene的架构图 由图可知搜索的过程如下: 用户输入搜索的关键字.对关键字进行分词.根据分词结果去索引库里面找到对应的文章id.根据文章id找到对应的文章 2. L ...
- iOS 单元测试之XCTest详解(一)
iOS 单元测试之XCTest详解(一) http://blog.csdn.net/hello_hwc/article/details/46671053 原创blog,转载请注明出处 blog.csd ...
- iOS学习之UINavigationController详解与使用(一)添加UIBarButtonItem
http://blog.csdn.net/totogo2010/article/details/7681879 1.UINavigationController导航控制器如何使用 UINavigati ...
- [转]iOS学习之UINavigationController详解与使用(三)ToolBar
转载地址:http://blog.csdn.net/totogo2010/article/details/7682641 iOS学习之UINavigationController详解与使用(二)页面切 ...
- iOS原生地图开发详解
在上一篇博客中:http://my.oschina.net/u/2340880/blog/414760.对iOS中的定位服务进行了详细的介绍与参数说明,在开发中,地位服务往往与地图框架结合使用,这篇博 ...
- [转]iOS学习之UINavigationController详解与使用(二)页面切换和segmentedController
转载地址:http://blog.csdn.net/totogo2010/article/details/7682433 iOS学习之UINavigationController详解与使用(一)添加U ...
随机推荐
- 实在解决不了丢失vs2019之类的msvcr110.dll之类的问题
因为msvcr110.dll也是微软DirectX的一个组件 如果在下载VC运行库没用的情况下,可能是因为要运行的程序是win32的,但是电脑和下载的程序是64的,所以 下载一个win32的即可 如果 ...
- Java数据结构-01顺序表
一.定义 线性表是一种线性结构,它是具有相同类型的n(n≥0)个数据元素组成的有限序列. 二.存储分类 1.顺序存储: ①简述:是指将线性表中的各个元素依次存放在一组地址连续的存储单元中,通常将这种方 ...
- webpack-从零搭建vuecli环境
模块化思想 // 1最早期就只是html和css处理网页 // 2发明一种语言来操作html和css js // 3早期只是在html文件里直接在script标签里写一些脚本代码 // 4随着Ajax ...
- 使用 Dockerfile 文件但是不使用缓存生成镜像
前一段时候使用 Dockerfile 重新部署 NetCore3.1 项目的时候很顺利,由来由于一些原因,我把以前的镜像删除,如果我们大家继续使用 docker build 命令去生成镜像的话就会报 ...
- 4G DTU无线数据透明传输终端
4G DTU是基于4G网络的远程无线数据透明传输终端,是一种物联网无线数据传输设备,使用公用运营商的4G网络为用户提供无线远距离数据传输功能,使用工业级32位的高性能通信处理器和工业级无线模块,以嵌入 ...
- Serilog 源码解析——总览
背景 大家好,考虑到在最近这些天,闲来无事,找了个类库好好研究下别人写的高质量代码,颇有收获,打算和大家分享下.考虑到最近在自学 ASP.NET Core 的相关开发,对 Serilog 这个日志记录 ...
- [P2114] [NOI2014]起床困难综合症 (位运算)
题面 传送门:https://www.luogu.org/problemnew/show/P2114 Solution 一道很有意思的位运算题. 要做这一题,我们首先得了解一个很重要的特点 位运算过程 ...
- 如何使用捷映App制作朋友圈九宫格视频图片合集
在刷朋友圈的过程中,看到九宫格的视频图片合集,带有酷炫的音效和动效,展现效果非常好,那么我们该如何制作呢?下面我为大家介绍. 第一步,我们需要在各大应用市场搜索[捷映视频制作]App,点击[安装]. ...
- 力扣 - 剑指Offer 35.复杂链表的复制
目录 题目 思路1 代码实现 思路2 代码实现 题目 请实现 copyRandomList 函数,复制一个复杂链表.在复杂链表中,每个节点除了有一个 next 指针指向下一个节点,还有一个 rando ...
- leetcode110:combination-sum-ii
题目描述 给出一组候选数C和一个目标数T,找出候选数中起来和等于T的所有组合. C中的每个数字在一个组合中只能使用一次. 注意: 题目中所有的数字(包括目标数T)都是正整数 组合中的数字 (a 1, ...