思路: 把UISearchVC作為tableView的頭視圖错负,只是顯示一個(gè)搜索框甘耿,當(dāng)點(diǎn)擊搜索框時(shí),可輸入文字進(jìn)行搜索么翰,根據(jù)searchVC的active屬性來在tabelView上顯示數(shù)據(jù)青灼。searchVC的代理方法暴心,當(dāng)搜索框內(nèi)容變化時(shí)會(huì)調(diào)用,可根據(jù)輸入內(nèi)容篩選搜索結(jié)果在tabelView上顯示杂拨。
代理方法:
-(void)updateSearchResultsForSearchController:(UISearchController *)searchController
效果圖:
效果圖.gif
代碼如下:
#import "MainViewController.h"
#define kScreenWidth [UIScreen mainScreen].bounds.size.width
#define kScreenHeight [UIScreen mainScreen].bounds.size.height
@interface MainViewController ()<UITableViewDelegate, UITableViewDataSource,UISearchResultsUpdating, UISearchBarDelegate>
@property (nonatomic, strong) NSMutableArray *allData;
@property (nonatomic, strong) NSMutableArray *searchData;
@property (nonatomic, strong) UITableView *myTable;
@property (nonatomic, strong) UISearchController *searchVC;
@end
@implementation MainViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.title = @"搜索";
self.allData = [NSMutableArray array];
self.searchData = [NSMutableArray array];
[self createTableView];
[self createSearchVC];
for (int i = 0; i < 100; i++) {
[self.allData addObject:[NSString stringWithFormat:@"%d", i]];
}
}
- (void)createTableView{
self.myTable = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
self.myTable.dataSource = self;
self.myTable.delegate = self;
[self.view addSubview:self.myTable];
}
-(void)createSearchVC{
self.searchVC = [[UISearchController alloc]initWithSearchResultsController:nil];
//設(shè)置searchVC的代理
self.searchVC.searchResultsUpdater = self;
//點(diǎn)擊搜索時(shí)是否加半透明遮罩視圖
self.searchVC.dimsBackgroundDuringPresentation = NO;
self.searchVC.searchBar.delegate = self;
//點(diǎn)擊搜索框時(shí)是否隱藏原來的導(dǎo)航欄
self.searchVC.hidesNavigationBarDuringPresentation = YES;
//設(shè)置搜索框上的文字
self.searchVC.searchBar.placeholder = @"搜索";
self.myTable.tableHeaderView = self.searchVC.searchBar;
//下面這兩行是設(shè)置 searchVC上searchBar后面的cancle改為取消
// [[UIBarButtonItem appearanceWhenContainedIn:[UISearchBar class], nil] setTitle:@"取消"];
[UIBarButtonItem appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]].title = @"取消";
}
//這個(gè)方法也能設(shè)置搜索框上的cancelBtn系顯示樣式
#pragma mark -UISearchBarDelegate
//- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar{
// [searchBar setShowsCancelButton:YES animated:YES];
//// searchBar.showsCancelButton = YES;
// for (id view in [[searchBar.subviews objectAtIndex:0] subviews]) {
// if ([view isKindOfClass:[UIButton class]]) {
//
// UIButton *btn = (UIButton *)view;
// [btn setTitle:@"取消" forState:UIControlStateNormal];
// break;
// }
// }
//}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
//設(shè)置區(qū)域的行數(shù)
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if (self.searchVC.active) {
return [self.searchData count];
}else{
return [self.allData count];
}
}
//返回單元格內(nèi)容
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *flag=@"cellFlag";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:flag];
if (cell==nil) {
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:flag];
}
if (self.searchVC.active) {
[cell.textLabel setText:self.searchData[indexPath.row]];
}
else{
[cell.textLabel setText:self.allData[indexPath.row]];
}
//
return cell;
}
#pragma mark -UISearchResultsUpdating
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController{
NSString *searchText = self.searchVC.searchBar.text;
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS %@", searchText];
if ([self.searchData count] != 0) {
[self.searchData removeAllObjects];
}
// 直接用賦值的方法专普,可變數(shù)組會(huì)變成不可變 這樣強(qiáng)轉(zhuǎn)searchData仍是不可變數(shù)組,再去調(diào)removeAllObjects會(huì)崩潰
// self.searchData = (NSMutableArray *)[self.allData filteredArrayUsingPredicate:predicate];
// 錯(cuò)誤提示: [__NSArrayI removeAllObjects]: unrecognized selector sent to instance
//以下三種方法是可以的
[self.searchData addObjectsFromArray:[self.allData filteredArrayUsingPredicate:predicate]];
// self.searchData = [[self.allData filteredArrayUsingPredicate:predicate] mutableCopy];
// self.searchData = [NSMutableArray arrayWithArray:[self.allData filteredArrayUsingPredicate:predicate]];
[self.myTable reloadData];
}
@end