分组表+本地搜索 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的更多相关文章

  1. iOS中的两种搜索方式UISearchDisplayController和UISearchController

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 以前iOS的搜索一般都使用UISearchDisplayCon ...

  2. iOS - UISearchController

    前言 NS_CLASS_DEPRECATED_IOS(3_0, 8_0, "UISearchDisplayController has been replaced with UISearch ...

  3. iOS - UITableViewController

    前言 NS_CLASS_AVAILABLE_IOS(2_0) @interface UITableView : UIScrollView <NSCoding> @available(iOS ...

  4. UISearchBar和 UISearchDisplayController的使用

    感觉好多文章不是很全面,所以本文收集整合了网上的几篇文章,感觉有互相补充的效果. 如果想下载源码来看:http://code4app.com/search/searchbar .本源码与本文无关 1. ...

  5. 如何在UINavigationBar上添加UISearchBar以及UISearchDisplayController的使用 --OC --iOS

    那我们开始吧,下面是Sely写的一个Demo,分享给大家. 新建一个项目, UISearchDisplayController 的 displaysSearchBarInNavigationBar太死 ...

  6. UISearchBar和UISearchDisplayController

    原文 http://hi.baidu.com/happywilma0118/item/e6d5730a499bba1b3a53eef8 UISearchBar继承自UIView.UIResponder ...

  7. UI UISearchBar UISearchDisplayController实现搜索条、解析颜色

    本文转载至 http://blog.sina.com.cn/s/blog_bf2d33bd01017q6l.html @interface ThirdViewController : UIViewCo ...

  8. 搜索框UISearchController的使用(iOS8.0以后替代UISearchBar + UISearchDisplayController)

    1.searchResultsUpdater:设置显示搜索结果的控制器 ? 1     _mySearchController.searchResultsUpdater = self; 2.dimsB ...

  9. iOS--- UITableView + UISearchDisplayController - - - - -实现搜索功能

    iOS中UISearchDisplayController用于搜索,搜索栏的重要性我们就不说了,狼厂就是靠搜索起家的,现在越来越像一匹没有节操的狼,UC浏览器搜索栏现在默认自家的神马搜索,现在不管是社 ...

随机推荐

  1. linux_环境变量设置 utf-8

    echo $LANG 显示编码  :  en_US.UTF-8  英文urf8有时显示程序输出是? 解决方法: vim ~/.bashrc 最后一行追加: export LANG=zh_CN.UTF- ...

  2. 百度地图API 添加自定义标注 多点标注

    原文:百度地图API 添加自定义标注 多点标注 分四个文件 location.php map.css 图片 数据库 数据库配置自己改下 -------------------------------- ...

  3. linux Packet socket (1)简单介绍

    本文主要来自于linux自带的man packet手冊: http://man7.org/linux/man-pages/man7/packet.7.html 平时常常使用的INET套接字提供的是7层 ...

  4. Oracle性能分析11:系统统计信息

    早期Oracle查询优化器的开销计算是基于运行SQL语句所须要的物理读,这种方法被叫做I/O开销模式(I/O cost model),这种方法的主要缺点是觉得单块读和多块读开销相当.在Oracle 8 ...

  5. UI 收集

    semantic http://www.semantic-ui.com.cn/modules/reveal.html sbadmin http://startbootstrap.com/templat ...

  6. JS代码的几个注意点规范

    也谈谈规范JS代码的几个注意点 也谈谈规范JS代码的几个注意点 写JS代码差不多也有两年了吧,从刚开始的“初生牛犊不怕虎”乱写一通到后来也慢慢知道去规范一下自己写的代码.这种感觉就像是代码是你的作品, ...

  7. ASP.NET 5:初始化数据库

    ASP.NET 5:初始化数据库 1.初始化数据库 1.2目录 这不是专述模式/架构设计的帖子,架构搭建以讲解文章为目的,先不过多分层. 截这个图也是便于你对应下面找代码文件路径! 1.2代码 先控制 ...

  8. JS分析URL字符串,取得参数名,AJAX传参请求代码示例

    //当前页面URL中参数分析函数,正则校验 function getQueryString(name) { var reg = new RegExp("(^|&)" + n ...

  9. C# 在本地创建文件夹及子文件夹

    string dict = @"d:\估价报告\"; if (!Directory.Exists(dict)) { Directory.CreateDirectory(dict); ...

  10. Smarty注释代码

    所有的smarty模板标签都被加上了定界符. 默认情况下是 { 和},但它们是可被改变的. 例如,我们假定你在使用默认定界符. 在smarty里,所有定界符以外的内容都是静态输出的,或者称之为不可改变 ...