您的位置:首页 > 产品设计 > UI/UE

UI之搜索框的创建Search

2015-11-15 15:24 375 查看
#import"RootViewController.h"
@interface
RootViewController ()<UITableViewDataSource,UITableViewDelegate,UISearchResultsUpdating>
//引入三个代理,一个用于搜索,两个用于建立cell
@property(nonatomic,strong)UITableView * tableV;
@property(nonatomic,strong)UISearchController * searchCon;
@property(nonatomic,strong)NSMutableArray * dataArr;//存源数据
@property(nonatomic,strong)NSMutableArray * searchArr;//存sear数据
@end
@implementationRootViewController
 
- (void)viewDidLoad {
    [super
viewDidLoad];
    self.title=@"搜索";
    self.searchArr=[NSMutableArraynew];
    [self
createDataArr];
    [self
createTable];
}
-(void)createDataArr
{
    self.dataArr=[NSMutableArraynew];
    for (int i=0; i<200; i++) {
        [self.dataArr
addObject:[NSString
stringWithFormat:@"%u",arc4random()%200]];
    }
}
-(void)createTable
{
    self.automaticallyAdjustsScrollViewInsets=NO;//
    self.tableV=[[UITableViewalloc]initWithFrame:CGRectMake(0,
64, [UIScreen
mainScreen].bounds.size.width, [UIScreen
mainScreen].bounds.size.height-64)
style:0];//设置表视图的范围
    self.tableV.delegate=self;
    self.tableV.dataSource=self;
    self.searchCon=[[UISearchControlleralloc]initWithSearchResultsController:nil];//创建搜索界面
    self.searchCon.searchResultsUpdater=self;//设置代理
    [self.searchCon.searchBarsizeToFit];//大小自适应,如果不设置则不显示搜索框
    self.searchCon.hidesNavigationBarDuringPresentation=NO;//点击搜索杠后是否隐藏导航条
    self.searchCon.dimsBackgroundDuringPresentation=NO;//点击搜索框后是否显示半透明灰色背景
    self.searchCon.searchBar.placeholder=@"请输入关键字....";//设置提示文字
    self.tableV.tableHeaderView=self.searchCon.searchBar;//把搜索条设为tableView的表头searchBar是searchController的属性,是一个搜索条
    [self.view
addSubview:self.tableV];
}
//UISearchResultsUpdating协议方法,点击searchBar会触发这个方法
-(void)updateSearchResultsForSearchController:(UISearchController *)searchController
{
    [self.searchArr
removeAllObjects];//把数组清空(删除上一次搜索的结果)
    NSPredicate* predicate=[NSPredicate
predicateWithFormat:@"SELFCONTAINS [c]%@",searchController.searchBar.text];//SELF CONTAINS[c]%@检索searchBar.text
   
    NSArray* array=[self.dataArrfilteredArrayUsingPredicate:predicate];//根据predicate进行检索(遍历查询)
    [self.searchArr
addObjectsFromArray:array];//把检索出来的数据存在searchArr里
    [self.tableV
reloadData];
}
-(NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.searchCon.active){//active活跃状态是否进入搜索状态
        returnself.searchArr.count;
    }
    return
self.dataArr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static
NSString * strid=@"id";
    UITableViewCell* cell=[tableView
dequeueReusableCellWithIdentifier:strid];
    if(cell==nil) {
        cell=[[UITableViewCellalloc]initWithStyle:0
reuseIdentifier:strid];
    }
    if (self.searchCon.active) {//显示搜索后的结果
        cell.textLabel.text=[NSString
stringWithFormat:@"%@",self.searchArr[indexPath.row]];
    }else
    {//显示全部的结果
        cell.textLabel.text=[NSString
stringWithFormat:@"%@",self.dataArr[indexPath.row]];
    }
    returncell;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: