首页
Web开发
Windows程序
编程语言
数据库
移动开发
系统相关
微信
其他好文
会员
首页
>
其他好文
> 详细
UISearchBar
时间:
2015-06-09 16:13:59
阅读:
94
评论:
0
收藏:
0
[点我收藏+]
标签:
#import "MDRootViewController.h"
@interface MDRootViewController ()<UITableViewDelegate,
UITableViewDataSource,UISearchBarDelegate,UISearchDisplayDelegate>
@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong)NSMutableArray *dataArr;
@property(nonatomic,strong)NSMutableArray *resultArr;
//搜索条
@property(nonatomic,strong)UISearchBar *searchBar;
//显示搜所结果的控制器
@property(nonatomic,strong)UISearchDisplayController *sdc;
@end
@implementation MDRootViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
#pragma mark - 创建数据
-(void)createData
{
if(!_dataArr)
_dataArr = [NSMutableArray array];
NSMutableArray *temp = [NSMutableArray array];
[temp addObject:@"超哥"];
[temp addObject:@"击飞"];
[temp addObject:@"飞哥"];
[temp addObject:@"亮哥"];
[self.dataArr addObject:temp];
//刷新数据
[self.tableView reloadData];
}
#pragma mark - UI
-(void)createUI
{
self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 568-64) style:UITableViewStylePlain];
self.tableView.dataSource = self;
self.tableView.delegate = self;
[self.view addSubview:self.tableView];
//创建搜索条
self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
self.searchBar.delegate = self;
self.tableView.tableHeaderView = self.searchBar;
//实例化搜索结果显示内容
self.sdc = [[UISearchDisplayController alloc] initWithSearchBar:self.searchBar contentsController:self];
//设置两个委托
self.sdc.searchResultsDataSource = self;
self.sdc.searchResultsDelegate = self;
//把结果存到数据中
if(!_resultArr)
_resultArr = [NSMutableArray array];
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self createUI];
[self createData];
}
#pragma mark - 黄金三问 dataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
//查找模式下就一个分段
if(tableView != self.tableView)
{
return 1;
}
else
{
//显示原始数据的分段
return [self.dataArr count];
}
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//显示搜索结果的状态
if(tableView != self.tableView)
{
if(_resultArr.count != 0)
[_resultArr removeAllObjects];
//开始查找
for(NSArray *subArr in self.dataArr)
{
for(NSString *string in subArr)
{
//比较字符串 输入内容
NSRange range = [string rangeOfString:self.searchBar.text];
//如果找到要搜索的数据
if(range.location != NSNotFound)
{
//把数据存到数组中
[self.resultArr addObject:string];
}
}
}
return [self.resultArr count];
}
else
{
//不是搜索状态显示原始数据
return [[self.dataArr objectAtIndex:section] count];
}
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//复用的方式就未注册过的方式
static NSString *cellName = @"Cell";
//创建一个可以复用的Cell表格
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellName];
//有可能会有空cell
if(!cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellName];
}
//状态判断
if(tableView != self.tableView)
{
//是搜索状态
cell.textLabel.text = [self.resultArr objectAtIndex:indexPath.row];
}
else
{
//没有处于搜索状态就显示数据源的数据
cell.textLabel.text = [[self.dataArr objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
}
//返回cell实例
return cell;
}
//点击按钮被取消
-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
NSLog(@"取消按钮被点击");
}
-(BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
{
NSLog(@"AAAA");
return YES;
}
UISearchBar
标签:
原文地址:http://www.cnblogs.com/4Dream/p/4563198.html
踩
(
0
)
赞
(
0
)
举报
评论
一句话评论(
0
)
登录后才能评论!
分享档案
更多>
2021年07月29日 (22)
2021年07月28日 (40)
2021年07月27日 (32)
2021年07月26日 (79)
2021年07月23日 (29)
2021年07月22日 (30)
2021年07月21日 (42)
2021年07月20日 (16)
2021年07月19日 (90)
2021年07月16日 (35)
周排行
更多
分布式事务
2021-07-29
OpenStack云平台命令行登录账户
2021-07-29
getLastRowNum()与getLastCellNum()/getPhysicalNumberOfRows()与getPhysicalNumberOfCells()
2021-07-29
【K8s概念】CSI 卷克隆
2021-07-29
vue3.0使用ant-design-vue进行按需加载原来这么简单
2021-07-29
stack栈
2021-07-29
抽奖动画 - 大转盘抽奖
2021-07-29
PPT写作技巧
2021-07-29
003-核心技术-IO模型-NIO-基于NIO群聊示例
2021-07-29
Bootstrap组件2
2021-07-29
友情链接
兰亭集智
国之画
百度统计
站长统计
阿里云
chrome插件
新版天听网
关于我们
-
联系我们
-
留言反馈
© 2014
mamicode.com
版权所有 联系我们:gaon5@hotmail.com
迷上了代码!