//  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. Deep Learning(深度学习)学习笔记整理系列之(五)

    Deep Learning(深度学习)学习笔记整理系列 zouxy09@qq.com http://blog.csdn.net/zouxy09 作者:Zouxy version 1.0 2013-04 ...

  2. 微信lbs---返回两个经纬度坐标点的距离

    微信开发:lbs附近的商家,在数据库里记录商家的坐标,lbs设置里管理搜索半径,查询的时候,查询 客户当前坐标的半径内的所有商家列表.个人喜欢不一样,我选择了执行sql ,毕竟效果高点.微信开发必须得 ...

  3. 使用ActivityGroup来切换Activity和Layout

    前言 在一个主界面中做Activity切换一般都会用TabActivity,使用方便,Activity互相之间相对独立,但是可定制性不强,而且修改起来很麻烦.当然也可以把layout分开,把逻辑代码全 ...

  4. iOS 在类实现定义中声明成员变量的怪异方式

    WebGL 规范(WebGL Specification) 太阳火神的漂亮人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商业用途-保持一致&quo ...

  5. SQLServer恢复表级数据

    最近几天,公司的技术维护人员频繁让我恢复数据库,因为他们总是少了where条件,导致update.delete出现了无法恢复的后果,加上那些库都是几十G.恢复起来少说也要十几分钟.为此,找了一些资料和 ...

  6. 可以将一些配置信息已json格式存在数据库中读取的时候序列化。

    public partial class json序列化 : System.Web.UI.Page    {        protected void Page_Load(object sender ...

  7. jQuery 的 live() 方法对 hover 事件的处理

    因为hover不是标准的事件,因此无法直接使用live进行处理,故使用以下方法代替,效果一样 <script type="text/javascript"> $(&qu ...

  8. jq实现搜索引擎的提示效果

    (function ($) { $.fn.Search = function (options) { var defaults = { inputid: "search", div ...

  9. Ubuntu下VSFTPD(四)(vsftpd其它主要设置)

    vsftpd 服务器的其它主要设置 : 1 .最大传输速率设置: 设置匿名用户的最大传输率为20Kbps,修改/etc/vsftpd/vsftpd.conf 添加语句:anon_max_rate=20 ...

  10. LeetCode24 Swap Nodes in Pairs

    题意: Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1-> ...