一迅栅、模糊查詢
第一種方法:UISearchController
1.代理協(xié)議 和 屬性 相關
代理協(xié)議:
UITableViewDelegate,UITableViewDataSource,UISearchControllerDelegate,UISearchResultsUpdating
屬性:
//tableView
@property (strong, nonatomic) UITableView *tableView;
//searchController
@property (strong, nonatomic) UISearchController *searchController;
//數(shù)據(jù)源
@property (strong,nonatomic) NSMutableArray *dataList;
@property (strong,nonatomic) NSMutableArray *searchList;
2.數(shù)組初始化和創(chuàng)建tableview searchcontroller
- (void)viewDidLoad {
[super viewDidLoad];
_dataList = [NSMutableArray array];
_searchList = [NSMutableArray array];
self.dataList=[NSMutableArray arrayWithCapacity:100];
//產(chǎn)生100個“數(shù)字+三個隨機字母”
for (NSInteger i=0; i<100; i++) {
[self.dataList addObject:[NSString stringWithFormat:@"%ld%@",(long)i,[self shuffledAlphabet]]];
}
//創(chuàng)建UISearchController
self.searchController = [[UISearchController alloc]initWithSearchResultsController:nil];
//設置代理
_searchController.delegate = self;
_searchController.searchResultsUpdater= self;
//設置UISearchController的顯示屬性口叙,以下3個屬性默認為YES
//搜索時,背景變暗色
_searchController.dimsBackgroundDuringPresentation = NO;
//搜索時雪位,背景變模糊
_searchController.obscuresBackgroundDuringPresentation = NO;
//隱藏導航欄
_searchController.hidesNavigationBarDuringPresentation = NO;
_searchController.searchBar.frame = CGRectMake(self.searchController.searchBar.frame.origin.x, self.searchController.searchBar.frame.origin.y, self.searchController.searchBar.frame.size.width, 44.0);
_searchController.searchBar.searchBarStyle = UISearchBarStyleMinimal;
_searchController.searchBar.backgroundColor = [UIColor whiteColor];
/// tableview
_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 20,WIDTH ,HEIGHT-64) style:UITableViewStylePlain];
_tableView.delegate = self;
_tableView.dataSource = self;
_tableView.separatorStyle = UITableViewCellSelectionStyleNone;
// _tableView.bounces = NO;
_tableView.showsVerticalScrollIndicator = NO;
[self.view addSubview:_tableView];
_tableView.tableHeaderView = _searchController.searchBar;
}
3.假數(shù)據(jù)隨機方法 和 相關代理
//產(chǎn)生3個隨機字母
- (NSString *)shuffledAlphabet {
NSMutableArray * shuffledAlphabet = [NSMutableArray arrayWithArray:@[@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H",@"I",@"J",@"K",@"L",@"M",@"N",@"O",@"P",@"Q",@"R",@"S",@"T",@"U",@"V",@"W",@"X",@"Y",@"Z"]];
NSString *strTest = [[NSString alloc]init];
for (int i=0; i<3; i++) {
int x = arc4random() % 25;
strTest = [NSString stringWithFormat:@"%@%@",strTest,shuffledAlphabet[x]];
}
return strTest;
}
//設置區(qū)域的行數(shù)
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if (self.searchController.active) {
return [self.searchList count];
}else{
return [self.dataList count];
}
}
//返回單元格內(nèi)容
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *flag=@"cell";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:flag];
if (cell==nil) {
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:flag];
}
if (self.searchController.active) {
[cell.textLabel setText:self.searchList[indexPath.row]];
}
else{
[cell.textLabel setText:self.dataList[indexPath.row]];
}
return cell;
}
#pragma mark - UISearchControllerDelegate代理
//測試UISearchController的執(zhí)行過程
- (void)willPresentSearchController:(UISearchController *)searchController
{
NSLog(@"willPresentSearchController");
}
- (void)didPresentSearchController:(UISearchController *)searchController
{
NSLog(@"didPresentSearchController");
}
- (void)willDismissSearchController:(UISearchController *)searchController
{
NSLog(@"willDismissSearchController");
}
- (void)didDismissSearchController:(UISearchController *)searchController
{
NSLog(@"didDismissSearchController");
}
- (void)presentSearchController:(UISearchController *)searchController
{
NSLog(@"presentSearchController");
}
-(void)updateSearchResultsForSearchController:(UISearchController *)searchController {
// 修改UISearchBar右側的取消按鈕文字顏色及背景圖片
searchController.searchBar.showsCancelButton = YES;
for (id searchbuttons in [[searchController.searchBar subviews][0] subviews]) //只需在此處修改即可
if ([searchbuttons isKindOfClass:[UIButton class]]) {
UIButton *cancelButton = (UIButton*)searchbuttons;
// 修改文字顏色
[cancelButton setTitle:@"返回"forState:UIControlStateNormal];
[cancelButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
}
NSLog(@"updateSearchResultsForSearchController");
NSString *searchString = [self.searchController.searchBar text];
NSPredicate *preicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[c] %@", searchString];
if (self.searchList!= nil) {
[self.searchList removeAllObjects];
}
//過濾數(shù)據(jù)
self.searchList= [NSMutableArray arrayWithArray:[_dataList filteredArrayUsingPredicate:preicate]];
//刷新表格
[self.tableView reloadData];
}
第二種方法:UISearchBar
1.代理和屬性
UITableViewDelegate,UITableViewDataSource,UISearchBarDelegate
//tableView
@property (strong, nonatomic) UITableView *tableView;
//searchBar
@property (nonatomic , strong) UISearchBar *search;
@property (nonatomic , strong) UIButton *searchCancleBtn;
//數(shù)據(jù)源
@property (strong,nonatomic) NSMutableArray *dataList;
@property (strong,nonatomic) NSMutableArray *searchList;
2.初始化
/** 搜索框背景圖片的封裝方法 */
- (UIImage *)imageWithColor:(UIColor *)color size:(CGSize)size{
CGRect recte = CGRectMake(0, 0, size.width, size.height);
UIGraphicsBeginImageContext(recte.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, recte);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
- (void)viewDidLoad {
[super viewDidLoad];
_dataList = [NSMutableArray array];
_searchList = [NSMutableArray array];
self.dataList=[NSMutableArray arrayWithCapacity:100];
//產(chǎn)生100個“數(shù)字+三個隨機字母”
for (NSInteger i=0; i<100 xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed i=0; xss=removed xss=removed> 0) {
return [self.searchList count];
}else{
return [self.dataList count];
}
}
//返回單元格內(nèi)容
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *flag=@"cell";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:flag];
if (cell==nil) {
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:flag];
cell.backgroundColor = [UIColor yellowColor];
}
if (self.inputString && self.inputString.length > 0) {
[cell.textLabel setText:self.searchList[indexPath.row]];
}
else{
[cell.textLabel setText:self.dataList[indexPath.row]];
}
return cell;
}
二、UISearchBar
第一種方式
1.創(chuàng)建UISearchBar
UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 72, WIDTH, HEIGHT/16+10)];
bgView.backgroundColor = WHITECOLOR;
[self.view addSubview:bgView];
UISearchBar *search = [[UISearchBar alloc]initWithFrame:CGRectMake(5, 5, WIDTH-10, HEIGHT/16)];
search.placeholder = @"搜索朋友";
search.backgroundColor = REDCOLOR;
search.backgroundImage = [self imageWithColor:WHITECOLOR size:search.bounds.size];
search.layer.borderColor = CAOGREENCOLOR.CGColor;
search.layer.borderWidth = 2;
search.layer.cornerRadius = 3;
search.clipsToBounds = YES;
[bgView addSubview:search];
2.搜索框表象封裝方法
/** 搜索框背景圖片的封裝方法 */
- (UIImage *)imageWithColor:(UIColor *)color size:(CGSize)size{
CGRect recte = CGRectMake(0, 0, size.width, size.height);
UIGraphicsBeginImageContext(recte.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, recte);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
self.inputString = searchText;
NSPredicate *preicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[c] %@", searchText];
if (self.searchList!= nil) {
[self.searchList removeAllObjects];
}
//過濾數(shù)據(jù)
self.searchList = [NSMutableArray arrayWithArray:[_dataList filteredArrayUsingPredicate:preicate]];
//刷新表格
[self.tableView reloadData];
}
第二種方式
//搜索框
self.search = [[UISearchBar alloc]initWithFrame:CGRectMake(100, 30, WIDTH-100-100, 30)];
self.search.placeholder = @"請搜索輸入";
self.search.searchBarStyle = UISearchBarStyleMinimal;
self.search.delegate = self;
self.navigationItem.titleView = self.search;
// [search setBackgroundImage:[[UIImage alloc] init]];
// [search setImage:[UIImage imageNamed:@"error.png"] forSearchBarIcon:UISearchBarIconClear state:UIControlStateNormal];