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的更多相关文章
随机推荐
- 【机器学习算法-python实现】决策树-Decision tree(1) 信息熵划分数据集
(转载请注明出处:http://blog.csdn.net/buptgshengod) 1.背景 决策书算法是一种逼近离散数值的分类算法,思路比較简单,并且准确率较高.国际权威的学术组织,数据挖掘国际 ...
- poj 3249 Test for Job (DAG最长路 记忆化搜索解决)
Test for Job Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 8990 Accepted: 2004 Desc ...
- 提高HTML5 canvas性能的几种方法
简介 HTML5 canvas 最初起源于苹果(Apple)的一项实验,现在已经成为了web中受到广泛支持的2D快速模式绘图(2Dimmediate mode graphic)的标准.许多开发者现在利 ...
- 在asp.net mvc中将checkbox传到后台时总是true的解决方法
我今天在做同城交友网站(www.niyeuwo.com)时发现,不管checkbox是否选 中,传到Controller时总是true,后来在查网上查了资料才知道,原来是jQuery在传值时写错了. ...
- 基于jQuery标题有打字效果的焦点图
给大家分享一款基于jQuery标题有打字效果的焦点图.之前为大家分享了好多jquery的焦点图.这款焦点图适用浏览器:IE8.360.FireFox.Chrome.Safari.Opera.傲游.搜狗 ...
- 小白日记44:kali渗透测试之Web渗透-SqlMap自动注入(二)-sqlmap参数详解REQUEST
Sqlmap自动注入(二) Request ################################################### #inurl:.php?id= 1. 数据段:--d ...
- kafka 简介
1. 概述 Kafka是Linkedin于2010年12月份开源的消息系统,它主要用于处理活跃的流式数据,使用scala编写.活跃的流式数据在web网站应用中非常常见,这些数据包括网站的pv.用户访 ...
- G - Not so Mobile
G - Not so Mobile Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Des ...
- Hadoop学习笔记(1) ——菜鸟入门
Hadoop学习笔记(1) ——菜鸟入门 Hadoop是什么?先问一下百度吧: [百度百科]一个分布式系统基础架构,由Apache基金会所开发.用户可以在不了解分布式底层细节的情况下,开发分布式程序. ...
- 改成 否“依然报LINK : fatal error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏”问题的解决
LINK : fatal error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏 . 这个bug说实话,还是挺让人蛋疼的!!! 问题发生原因: 我这里能跑2013+opencv24 ...