UI1_UITableViewSearchController
// 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的更多相关文章
随机推荐
- C#利用lambda在函数中创建内部函数
有使用过JS的朋友,相信都知道function.JS中的function是可以在里面在定义一个作为内部使用的.有时为了控制作用域,或者这种小函数只在这个函数体内会使用,所以就不希望在外部在作额外的定义 ...
- UITableViewController
UITableViewController 目录 概述 UITableView UITableViewCell 与UITableViewController相关的代理 UITableViewDataS ...
- 深入解析ext2文件系统之mke2fs
上一遍博文的重点其实将ext2整体的组织框架,我们知道了ext2文件系统由块组组成,每个块组里面的组织形式.我们甚至直接把超级块和组描述符里面的内容,用十六进制形式展现了出来.这篇博文主要讲述如何 ...
- archlinux随记
xrdb -merge .Xresources才能使urxvt的配色显示正常 xpmroot 包含在fvwm包中
- 代码片段------find批量处理
应用案例: 当前目录下有若干目录,有多个目录下有一个名叫build.sh的脚本,但是没有可执行权限,现在要将所有的build.sh假如可执行权限. pengdl@debian:~/work/SDK_3 ...
- 3.2html学习笔记之图片
<img src="" width="50%" alt="加载时候或无法显示时候显示的文字" height="让浏览器预先给 ...
- 从零开始学JAVA(02)-用Eclipse写hello World
在安装好JAVA开发环境的前提下开始以下工作,以下文章参考http://blog.csdn.net/ojtojt/article/details/3476157文章,进行测试编写日记,内容版权归原作者 ...
- [数据结构]字典树(Tire树)
概述: Trie是个简单但实用的数据结构,是一种树形结构,是一种哈希树的变种,相邻节点间的边代表一个字符,这样树的每条分支代表一则子串,而树的叶节点则代表完整的字符串.和普通树不同的地方是,相同的字符 ...
- 【Android Studio使用教程 7】AndroidStudio问题汇总
FindBugs:Compiler output path for module can not be null. check your module/project settings问题原因 And ...
- A的href和onclick
我们在制作网页的时候用得最多的就是 A 标签 超连接 但有时候需要在 A 里同时用到 href 和 onclick属性 下面我们先来分析一下 ,在不同的浏览器下有不同的效果 1 顺序 ie 6 : h ...