//  UI1_UITableViewSearchController
//
// Created by zhangxueming on 15/7/15.
// Copyright (c) 2015年 zhangxueming. All rights reserved.
// #import "AppDelegate.h"
#import "ViewController.h" @interface AppDelegate () @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
ViewController *root = [[ViewController alloc] init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:root];
self.window.rootViewController = nav;
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeK
//
// ViewController.m
// UI1_UITableViewSearchController
//
// Created by zhangxueming on 15/7/15.
// Copyright (c) 2015年 zhangxueming. All rights reserved.
// #import "ViewController.h" @interface ViewController ()
{
UITableView *_tableView;//表视图
NSMutableArray *_dataList;//数据源
//UISearchDisplayController
UISearchController *_searchController;//搜索控制器
NSMutableArray *_searchList;//搜索的结果
}
@end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self createData];
[self createUI];
} - (void)createData
{
_dataList = [NSMutableArray array];
_searchList = [NSMutableArray array];
for (int i='A'; i<='Z'; i++) {
NSMutableArray *array = [NSMutableArray array];
for (int j=0; j<5; j++) {
NSInteger num = arc4random()%50+1;
NSString *name = [NSString stringWithFormat:@"人物%d:name%ld",j,num];
[array addObject:name];
}
[_dataList addObject:array];
}
} - (void)createUI
{
_tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
_tableView.delegate = self;
_tableView.dataSource = self;
[self.view addSubview:_tableView]; //创建搜索控制器, nil 表示在当前view上显示搜索结果
_searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
//更新搜索结果
_searchController.searchResultsUpdater = self;
//隐藏导航栏
_searchController.hidesNavigationBarDuringPresentation = NO;
//背景变暗
_searchController.dimsBackgroundDuringPresentation = NO;
//添加searchBar
CGRect frame = _searchController.searchBar.frame;
frame.size.height = 44;
_searchController.searchBar.frame = frame;
_tableView.tableHeaderView = _searchController.searchBar;
} #pragma mark ---UITableView代理--- //返回分区的个数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
if(_searchController.active)
{
return 1;
}
else
{
return _dataList.count;
}
}
//返回分区中的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//_dataList[section] <==> [_dataList objectAtIndex:section];
if (_searchController.active) {
return _searchList.count;
}
return [_dataList[section] count];
} //创建cell - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellId = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellId];
} cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
if (_searchController.active) {
cell.textLabel.text = _searchList[indexPath.row];//[_searchList objectAtIndex:indexPath.row];
}
else
{
cell.textLabel.text = _dataList[indexPath.section][indexPath.row];
}
return cell;
} //设置分区的头标题 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
if (_searchController.active) {
return @"搜索结果";
}
return [NSString stringWithFormat:@"%c",(char)(section+'A')];
}
//设置分区头的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 50;
} //设置分区索引
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
if(_searchController.active)
{
return nil;
}
NSMutableArray *titles = [NSMutableArray array];
[titles addObject:UITableViewIndexSearch];
for (int i='A'; i<='Z'; i++) {
[titles addObject:[NSString stringWithFormat:@"%c",(char)i]];
}
return titles;
} - (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
return index-1;
} //更新搜索结果
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController
{
[_searchList removeAllObjects];
//NSPredicate 谓词语法
//过滤数据
NSString *searchString = _searchController.searchBar.text;
for (int i=0; i<_dataList.count; i++) {
NSInteger count = [[_dataList objectAtIndex:i] count];
for (NSInteger j=0; j<count; j++) {
NSString *obj = [[_dataList objectAtIndex:i] objectAtIndex:j];
if ([obj containsString:searchString]) {
[_searchList addObject:obj];
}
}
}
[_tableView reloadData];
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end
eyAndVisible]; return YES; } // // ViewController.h // UI1_UITableViewSearchController // // Created by zhangxueming on 15/7/15. // Copyright (c) 2015年 zhangxueming. All rights reserved. // #import <UIKit/UIKit.h> @interface ViewController : UIViewController <UITableViewDelegate, UITableViewDataSource,UISearchResultsUpdating> @end

UI1_UITableViewSearchController的更多相关文章

随机推荐

  1. 使用generator自动生成Mybatis映射配置文件

    在使用mybatis时,映射文件的配置非常麻烦,对于做逻辑不是很复杂,功能不是特别关键的模块的时候,我们没有必要手动书写,可以使用generator工具生成. generator工具实际上就是根据数据 ...

  2. 将 Android 应用移植到 BlackBerry PlayBook 上

    美国西部时间18号早上,也就是我们的19号凌晨,BlackBerry DevCon活动隆重举行,PlayBook 2.0开发测试版随之发布.PlayBook 2.0的一个重要功能就是支持Android ...

  3. TOJ3651确定比赛名次

    确定比赛名次   Time Limit(Common/Java):1000MS/3000MS     Memory Limit:65536KByte Total Submit: 23          ...

  4. C++调用C#库简单例程

    有些时候,为了使用别人已经写好的C#库文件,我们需要使用C++调用C#库文件: 以下做了一简单的调用工程,步骤如下: 1.准备C#库 (dll文件)   1)创建C#库:     2)编写C#类:   ...

  5. 关于Java文件删除的操作

    在调用Java删除文件时,我们常常会调用file.delete();,但其实这个delete只能删除单个文件还有空文件夹,但是如果文件夹不是空,这个方法就无法删除文件了. 所以要删除有内容的文件夹,就 ...

  6. Python Counter() 的实现

    Table of Contents 1. collections.Counter 源码实现 1.1. __init__ 1.2. update 1.3. most_common 1.3.1. item ...

  7. 去掉或者修改 input、select 等表单的【默认样式 】

    隐藏input等表单的默认样式的背景: textarea,select,input{-webkit-appearance: none; -moz-appearance: none; -o-appear ...

  8. WPF 之 style文件的引用

    总结一下WPF中Style样式的引用方法. 一.内联样式: 直接设置控件的Height.Width.Foreground.HorizontalAlignment.VerticalAlignment等属 ...

  9. Solr 删除数据的几种方式

    原文出处:http://blog.chenlb.com/2010/03/solr-delete-data.html 有时候需要删除 Solr 中的数据(特别是不重做索引的系统中,在重做索引期间).删除 ...

  10. iOS开发— Socket编程

    Socket编程 一.了解网络各个协议:TCP/IP.SOCKET.HTTP等 网络七层由下往上分别为物理层.数据链路层.网络层.传输层.会话层.表示层和应用层. 其中物理层.数据链路层和网络层通常被 ...