//  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. Java Swing 探索(一)LayoutManager

    BorderLayout FlowLayout GridLayout GridBagLayout CardLayout BoxLayout 1.BorderLayout java.lang.Objec ...

  2. 源码分析:静态分析 C 程序函数调用关系图

    http://www.tinylab.org/callgraph-draw-the-calltree-of-c-functions/

  3. [Oracle] Oracle和SQLServer的数据类型比较

    类型名称 Oracle SQLServer 比较 字符数据类型 CHAR CHAR 都是固定长度字符资料但oracle里面最大度为2kb,SQLServer里面最大长度为8kb 变长字符数据类型 VA ...

  4. Oracle基础 数据库备份和恢复

    一.为什么需要数据备份 造成数据丢失的主要原因: 1.介质故障. 2.用户的错误操作. 3.服务器的彻底崩溃. 4.计算机病毒. 5.不可预料的因素. Oracle中故障类型分为以下4种. 1.语句故 ...

  5. React Native中常见操作

    一.如何将项目安装到iOS手机上? 1.将手机于电脑连接好,并将Xcode打开连接上手机 2.在General  -> Team 下添加自己的Apple ID. 3.改本地的IP地址: 4.点击 ...

  6. js中Math()函数&&数据类型转换

    Math()函数: x的y次方:Math.pow(x,y) 取小数点后两位:num.toFixed(2) 数据类型转换: 字符串转换为数字:parseInt(num)转换为整数:parseFloat( ...

  7. 剑指Offer25 二叉搜索树转换为排序双向链表

    /************************************************************************* > File Name: 25_BSTCon ...

  8. python 打包exe注意的问题

    教程百度.谷歌都有,本文讲讲安装时出错的问题. 教程:http://keliang.blog.51cto.com/3359430/661884 1.cxfreeze 找不到路径时: 需要修改Scrip ...

  9. hdu 3308 LCIS 线段树

    昨天热身赛的简单版:LCIS.昨天那题用树链剖分,不知道哪里写错了,所以水了水这题看看合并.更新方式是否正确,发现没错啊.看来应该是在树链剖分求lca时写错了... 题目:给出n个数,有两种操作: 1 ...

  10. BZOJ 3713

    Description 斐波那契数列的定义为:k=0或1时,F[k]=k:k>1时,F[k]=F[k-1]+F[k-2].数列的开头几项为0,1,1,2,3,5,8,13,21,34,55,…你 ...