iOS UISearchController 搜索框
Posted 唐唐_010
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS UISearchController 搜索框相关的知识,希望对你有一定的参考价值。
#import <Foundation/Foundation.h>
@interface Student : NSObject
@property(strong,nonatomic) NSString *name;
@property(strong,nonatomic) NSString *pic;
@property(strong,nonatomic) NSString *tel;
-(Student *)initWithDic:(NSDictionary *)dic;
+(Student *)studentWithDic:(NSDictionary *)dic;
@end
#import "Student.h"
@implementation Student
-(Student *)initWithDic:(NSDictionary *)dic
{
self= [super init];
if (self)
{
self.name=dic[@"name"];
self.pic=dic[@"pic"];
self.tel=dic[@"tel"];
}
return self;
}
+(Student *)studentWithDic:(NSDictionary *)dic
{
return [[self alloc] initWithDic:dic];
}
-(NSString *)description
{
return [NSString stringWithFormat:@"name=%@,pic=%@,tel=%@",_name,_pic,_tel];
}
@end
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate,UISearchResultsUpdating>
@property(strong,nonatomic) NSMutableArray *arrData;
@property(strong,nonatomic) UITableView *tableV;
@property(strong,nonatomic) UISearchController *searchController;
@property(strong,nonatomic) NSArray *arrTemp;
@end
#import "ViewController.h"
#import "Student.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.arrData=[NSMutableArray array];
for (NSArray *arr in [[NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"GYiosclass(2)" ofType:@"plist"]] allValues])
{
for (NSDictionary *dic in arr)
{
[self.arrData addObject:[Student studentWithDic:dic]];
}
}
self.tableV=[[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
self.tableV.delegate=self;
self.tableV.dataSource=self;
[self.view addSubview:self.tableV];
self.searchController=[[UISearchController alloc] initWithSearchResultsController:nil];
//指定代理
self.searchController.searchResultsUpdater=self;
//搜索框的背景色
self.searchController.searchBar.backgroundColor=[UIColor lightGrayColor];
//将
self.tableV.tableHeaderView=self.searchController.searchBar;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (self.searchController.active)
{
return self.arrTemp.count;
}
else
{
return self.arrData.count;
}
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"MyCell"];
if (cell==nil)
{
cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"MyCell"];
}
if (self.searchController.active)
{
cell.textLabel.text=[self.arrTemp[indexPath.row] name];
cell.detailTextLabel.text = nil;
cell.imageView.image=nil;
}
else
{
cell.textLabel.text=[self.arrData[indexPath.row] name];
cell.imageView.image=[UIImage imageNamed:[self.arrData[indexPath.row] pic]];
cell.detailTextLabel.text=[self.arrData[indexPath.row] tel];
}
return cell;
}
//实现UISearchResultsUpdating 代理
-(void)updateSearchResultsForSearchController:(UISearchController *)searchController
{
//谓词
NSPredicate *pre=[NSPredicate predicateWithFormat:@"name=%@",self.searchController.searchBar.text];
NSLog(@"%@",pre);
self.arrTemp=[self.arrData filteredArrayUsingPredicate:pre];
dispatch_async(dispatch_get_main_queue(),^
{
[self.tableV reloadData];
});
NSLog(@"%@",self.arrTemp);
}
//
-(void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
if (self.searchController.active)
{
self.searchController.active=NO;
[self.searchController.searchBar removeFromSuperview];
}
}
@end
以上是关于iOS UISearchController 搜索框的主要内容,如果未能解决你的问题,请参考以下文章
UISearchController:昏暗的覆盖覆盖搜索栏本身
iOS UISearchController Class Reference
ios UIsearchcontroller 中的搜索栏消失