UISearchDisplayController UISearchBar
分组表+本地搜索 UISearchDisplayController UISearchBar 的使用
效果图
@interface CityListViewController :UIViewController<UITableViewDataSource,UITableViewDelegate,UISearchBarDelegate>
@property (nonatomic, retain) UITableView*mTableView;
@property (nonatomic, retain) NSArray*dataList;
@property (nonatomic, retain) NSArray*searchData;
@property (nonatomic, retain)NSMutableArray *allCitys;
@property (nonatomic, retain) UISearchBar*mSearchBar;
@property (nonatomic, retain)UISearchDisplayController *searchController;
@end
#import"CityListViewController.h"
#import "AppDelegate.h"
- (void)viewDidLoad
{
//初始化分组表
self.mTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 88,320, 480) style:UITableViewStyleGrouped];
self.mTableView.delegate = self;
self.mTableView.dataSource = self;
[self.mTableViewsetContentSize:CGSizeMake(320, 3000)];
//初始化搜索条
self.mSearchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 44, 320, 44)];
[self.mSearchBarsetBackgroundImage:[UIImage imageNamed:@"nav_bg.png"]];
[self.mSearchBar setPlaceholder:@"搜索城市"];
self.mSearchBar.delegate = self;
[self.mSearchBar sizeToFit];
//初始化UISearchDisplayController
self.searchController =[[UISearchDisplayController alloc] initWithSearchBar:self.mSearchBarcontentsController:self];
self.searchController.searchResultsDelegate= self;
self.searchController.searchResultsDataSource = self;
self.searchController.delegate = self;
//解析数据源文件
NSString *path = [[NSBundle mainBundle]pathForResource:@"Provineces" ofType:@"plist"];
self.dataList = [NSMutableArrayarrayWithContentsOfFile:path];
//确定每个分组的行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if([tableViewisEqual:self.searchController.searchResultsTableView]){
NSLog(@"searchData count is%d",[self.searchData count]);
return [self.searchData count];
}
else{
NSDictionary *dic = [self.dataListobjectAtIndex:section];
NSArray *cityCount = [dicobjectForKey:@"Citys"];
int count = (int)[cityCount count];
for(int i = 0;i<count-1;i++){
NSDictionary *d = [cityCountobjectAtIndex:i];
NSString *Name = [dobjectForKey:@"C_Name"];
[self.allCitys addObject:Name];
}
return [cityCount count];
}
}
//分组的个数
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
if([tableViewisEqual:self.searchController.searchResultsTableView])
return 1;
else
return [self.dataList count];
}
//每个分组的Header
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
NSString *HeaderName;
if([tableViewisEqual:self.searchController.searchResultsTableView]){
HeaderName = @"搜索结果";
}else{
NSDictionary *dict = [self.dataListobjectAtIndex:section];
HeaderName = [dictobjectForKey:@"p_Name"];
return HeaderName;
}
return HeaderName;
}
#pragma mark - UISearchDisplayControllerdelegate methods
-(void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope {
NSMutableArray *searchResult =[[NSMutableArray alloc] initWithCapacity:0];
int j =(int) [self.allCitys count];
for (int i = 0; i<j-1; i++) {
NSString *str = [self.allCitysobjectAtIndex:i];
if([strrangeOfString:searchText].location != NSNotFound )
{
[searchResult addObject:str];
}
}
self.searchData = [NSArrayarrayWithArray:searchResult];
[searchResult release];
}
-(BOOL)searchDisplayController:(UISearchDisplayController*)controller shouldReloadTableForSearchString:(NSString *)searchString {
[selffilterContentForSearchText:searchString
scope:[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];
return YES;
}
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption {
[selffilterContentForSearchText:[self.searchDisplayController.searchBar text] scope:[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:searchOption]];
return YES;
}
//tableView cell刷新数据
-(UITableViewCell *) tableView:(UITableView *)tableViewcellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellName =@"cellName";
UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:cellName];
if(cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellName];
}
if([tableViewisEqual:self.searchController.searchResultsTableView]){
cell.textLabel.text = [self.searchDataobjectAtIndex:indexPath.row];
}else{
NSDictionary *dict = [self.dataListobjectAtIndex:[indexPath section]];
NSArray *shengfen = [dictobjectForKey:@"Citys"];
NSDictionary *citys = [shengfenobjectAtIndex:indexPath.row];
NSString *Name = [citysobjectForKey:@"C_Name"];
cell.textLabel.text = Name;
//[self.allCitys addObject:Name];
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
if([tableViewisEqual:self.searchController.searchResultsTableView]){
self.cityName = [self.searchDataobjectAtIndex:indexPath.row];
((AppDelegate *)[[UIApplicationsharedApplication]delegate]).APPDelegateCityName = self.cityName;
}else{
self.cityName = [[[[self.dataListobjectAtIndex:indexPath.section] objectForKey:@"Citys"]objectAtIndex:indexPath.row] objectForKey:@"C_Name"];
((AppDelegate *)[[UIApplicationsharedApplication]delegate]).APPDelegateCityName = self.cityName;
}
[self.navigationControllerpopToRootViewControllerAnimated:YES];
}
UISearchDisplayController UISearchBar的更多相关文章
- iOS中的两种搜索方式UISearchDisplayController和UISearchController
大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 以前iOS的搜索一般都使用UISearchDisplayCon ...
- iOS - UISearchController
前言 NS_CLASS_DEPRECATED_IOS(3_0, 8_0, "UISearchDisplayController has been replaced with UISearch ...
- iOS - UITableViewController
前言 NS_CLASS_AVAILABLE_IOS(2_0) @interface UITableView : UIScrollView <NSCoding> @available(iOS ...
- UISearchBar和 UISearchDisplayController的使用
感觉好多文章不是很全面,所以本文收集整合了网上的几篇文章,感觉有互相补充的效果. 如果想下载源码来看:http://code4app.com/search/searchbar .本源码与本文无关 1. ...
- 如何在UINavigationBar上添加UISearchBar以及UISearchDisplayController的使用 --OC --iOS
那我们开始吧,下面是Sely写的一个Demo,分享给大家. 新建一个项目, UISearchDisplayController 的 displaysSearchBarInNavigationBar太死 ...
- UISearchBar和UISearchDisplayController
原文 http://hi.baidu.com/happywilma0118/item/e6d5730a499bba1b3a53eef8 UISearchBar继承自UIView.UIResponder ...
- UI UISearchBar UISearchDisplayController实现搜索条、解析颜色
本文转载至 http://blog.sina.com.cn/s/blog_bf2d33bd01017q6l.html @interface ThirdViewController : UIViewCo ...
- 搜索框UISearchController的使用(iOS8.0以后替代UISearchBar + UISearchDisplayController)
1.searchResultsUpdater:设置显示搜索结果的控制器 ? 1 _mySearchController.searchResultsUpdater = self; 2.dimsB ...
- iOS--- UITableView + UISearchDisplayController - - - - -实现搜索功能
iOS中UISearchDisplayController用于搜索,搜索栏的重要性我们就不说了,狼厂就是靠搜索起家的,现在越来越像一匹没有节操的狼,UC浏览器搜索栏现在默认自家的神马搜索,现在不管是社 ...
随机推荐
- OWIN– 解耦,协作和开放
OWIN的理解和实践(一) – 解耦,协作和开放 概述 OWIN的全称是Open Web Interface For .Net, 是MS在VS2013期间引入的全新的概念, 网上已经有不少的关于它的信 ...
- 音乐TV2015校园招聘A第二大发行量(对中国科学院大学站)
标题叙述性说明:鉴于阵列A,尺寸n,数组元素1至n数位,但是,一些数字多次出现,有些数字不出现.请设计算法和程序.统计数据不会出现什么.什么号码多次出现. 您可以O(n)时间复杂度,O(1)求下完毕么 ...
- pyqt学习总结
一.学习来由: 近期一段时间,应朋友的须要,完毕一款抓取软件.一般而言,python是我比較熟悉的语言,又有丰富的抓取和解析模块,所以果断选择之. 而这远远不是重点,后台程序在工作做常常写,所以比較熟 ...
- view components介绍
view components介绍 在ASP.NET MVC 6中,view components (VCs) 功能类似于虚拟视图,但是功能更加强大. VCs兼顾了视图和控制器的优点,你可以把VCs ...
- Android注入事件的三种方法比较
方法1:使用内部APIs 该方法和其他所有内部没有向外正式公布的APIs一样存在它自己的风险.原理是通过获得WindowManager的一个实例来访问injectKeyEvent/injectPoin ...
- W5500问题集锦(一)
在"WIZnet杯"以太网技术竞赛中,有非常多參赛者在使用中对W5500有各种各样的疑问,对于这款WIZnet新推出的以太网芯片,使用中大家是不是也一样存在下面问题呢?来看一看: ...
- 快速构建Windows 8风格应用7-页面视图概览
原文:快速构建Windows 8风格应用7-页面视图概览 本篇博文主要介绍Windows 8风格应用中包含哪些视图.Visual Studio 2012和模拟器中如何开发和调试不同的页面视图.页面视图 ...
- Spring Resources之介绍和资源接口
1.介绍 不幸的是Java的标准的java.net.URL类和针对不同的URL前缀的标准处理器都不够充分去访问所有的低级资源.例如,美誉标准化的URL实现可能用于去范围需要从classpath中或者相 ...
- HDU 2064 汉诺塔III
汉诺塔III Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submis ...
- Java集合之LinkedHashSet源码分析
1.简介 我们知道Set不允许包含相同的元素,如果试图把两个相同元素加入同一个集合中,add方法返回false.根据源码实现中的注释我们可以知道LinkedHashSet是具有可预知迭代顺序的Set接 ...