IOS:将搜索栏添加到表视图
来源:互联网 发布:数据过滤算法 编辑:程序博客网 时间:2024/04/29 15:29
首先创建一个单视图工程,在ViewController.h中代码如下:
#import <UIKit/UIKit.h>
@interface ViewController :
UIViewController<UITableViewDelegate,UITableViewDataSource,UISearchBarDelegate>
@property(retain,nonatomic)UITableView *mTableView;
@property(retain,nonatomic)UITableView *pMtableView;
@property(retain,nonatomic)UISearchBar *mSearcherBar;
@property(retain,nonatomic)NSMutableArray *mArr1;
@property(retain ,nonatomic)NSMutableArray *mArr2;
@end
在ViewController.h中代码如下:
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[superviewDidLoad];
//创建初始化一个表视图
UITableView *pTable=[[UITableViewalloc]initWithFrame:self.view.framestyle:UITableViewStylePlain];
self.mTableView = pTable;
[pTable release];
//创建初始化一个搜索框
self.mSearcherBar = [[UISearchBaralloc]initWithFrame:CGRectMake(0,0,self.view.frame.size.width,30)];
//将表视图的表头设为搜索框
self.mTableView.tableHeaderView =self.mSearcherBar;
//设置代理
self.mTableView.delegate =self;
self.mTableView.dataSource =self;
self.mSearcherBar.delegate =self;
//将表视图添加到当前时图
[self.viewaddSubview:self.mTableView];
//初始化可变数组
self.mArr1 = [[NSMutableArrayalloc]initWithCapacity:60];
self.mArr2 = [[NSMutableArrayalloc]initWithCapacity:60];
//给可变数组赋值
for(int i=0;i<60;i++)
{
NSString *pstr=[NSStringstringWithFormat:@"%d",i];
[self.mArr1addObject:pstr];
[self.mArr2addObject:pstr];
}
}
#pragma ------------------------DataSource Delegate----------------------------
//设置每行的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 50;
}
//设置每个分组表头的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 20;
}
//设置每个分组表尾的高度
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return 20;
}
#pragma -----------------------------------UITableView Delegate------------------
//必须实现的两个协议
//返回每个分组包含的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.mArr2count];
}
//设置每行内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *pstr=@"hello";
UITableViewCell *pCell=[tableViewdequeueReusableCellWithIdentifier:pstr];
if(pCell == nil)
{
pCell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:pstr];
}
NSUInteger cellRow = [indexPath row];
pCell.textLabel.text = [self.mArr2objectAtIndex:cellRow];
pCell.textLabel.textColor = [UIColorgreenColor];
pCell.detailTextLabel.text =@"detailTextField";
pCell.detailTextLabel.textColor = [UIColorblackColor];
return pCell;
}
//可以选择实现的协议
//设置分组数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 3;
}
//设置表头标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return@"Header";
}
//设置表尾的标题
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
return@"Footer";
}
#pragma ---------------------------------UISearcherBar Delegate------------------
//在搜索框内搜索内容,并展示搜索结果
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
//移除mArr2中所有元素,来存放搜索结果
[self.mArr2removeAllObjects];
//对数组mArr1遍历,看数组中是否包含搜索框里的内容
for(NSString *pstrinself.mArr1)
{
//如果包含搜索框里的内容,就把数组中的元素添加到mArr2中
if([pstr hasPrefix:searchBar.text])
{
[self.mArr2addObject:pstr];
}
}
[self.mSearcherBarresignFirstResponder];
//展示搜索结果
[self.mTableViewreloadData];
}
- (void)didReceiveMemoryWarning
{
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
- IOS:将搜索栏添加到表视图
- IOS之表视图添加搜索栏
- IOS-表视图中添加搜索功能
- IOS-表视图中添加搜索功能
- IOS-表视图中添加搜索功能
- IOS编程教程: storyboard如何往表视图(Table View)添加搜索栏(Search Bar)
- IOS编程教程(十三):如何往表视图(Table View)添加搜索栏(Search Bar)
- IOS编程教程(十三):如何往表视图(Table View)添加搜索栏(Search Bar)
- IOS 带搜索栏表视图
- 【一步一步学IOS5 】 在表视图中添加搜索栏
- 如何在表视图中添加搜索
- 只将unity的UI控件添加到iOS视图层上,不显示unity原生的背景(一)
- 只将unity的UI控件添加到iOS视图层,不显示unity的原生背景(二)
- 将自定义域加入到视图---添加任务信息到项目中心视图
- solr搜索提示,将词添加到词库中
- iOS添加一个view到所有视图的上面
- JS视图搜索功能(将检索条件赋到url中传值)
- iOS将文字添加到图片上的方法
- 第十七周项目2-1-字符串连接(指针做形参)
- Android动画系列(二):Tween动画
- adjustsFontSizeToFitWidth:
- 不一样的Javascript(10)——函数嵌套
- Java 重定向标准输入输出流
- IOS:将搜索栏添加到表视图
- Ubuntu下安装LAMP及phpmyadmin
- 软件开发常用英语词汇
- 第六章 注解式控制器详解
- UBIFS文件系统简介
- 405 delete method not allowed 解决方案
- 你爱的圣诞礼物,网络钓客也爱,年底网络钓鱼最爱购物页面
- Eclipse远程调式
- 第十七周 项目1