·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> app软件开发 >> IOS开发 >> UITableView的搜索:SearchBar和SearchDisplayController;UISearchController

UITableView的搜索:SearchBar和SearchDisplayController;UISearchController

作者:佚名      IOS开发编辑:admin      更新时间:2022-07-23

之前我写过一篇关于在tableView中利用谓词搜索的随笔,当时使用的是自定义textField,最近在写电子书,需要在书目时实现搜索功能.所以在此把以前用于实现搜索功能系统提供的的SearchBar和SearchDisplayController的方法在这里和大家分享一下.不过在iOS8滞后,苹果已经不再推荐我们是使用这两个旧东西,而是让我们使用UISearchController,自然我也会把这个新东西的用法在这里演示一下.

 

SearchBar和SearchDisplayController
先在视图xib文件中添加tableView,在表上面添加SearchBar和SearchDisplayController.如图:

 

需要用到的协议有:<UITableViewDataSource,UITableViewDelegate,UISearchBarDelegate,UISearchDisplayDelegate>

我在这里声明了两个数据,用来保存搜索数据和搜索结果:

@PRoperty(nonatomic,retain)NSMutableArray *dataArray;
@property(nonatomic,retain)NSMutableArray *resultArray;

初始化数组:
self.dataArray=[[NSMutableArray alloc]initWithObjects:@"张三",@"李四",@"王五",@"阿五",@"云飞",@"asd", nil];

 

在这里其实有两个表,一个是搜索前的所有数据,另一个是展示搜索结果,所以在写tabbleView的协议时需要进行区分:

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView==self.tableView) {
        return self.dataArray.count;
    }
    else
    {
        return self.resultArray.count;
    }
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *str=@"cell";
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:str];
    if (cell==nil) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str];
    }
    if (tableView==self.tableView) {
        cell.textLabel.text=self.dataArray[indexPath.row];
    }
    else
    {
        cell.textLabel.text=self.resultArray[indexPath.row];
    }
    return cell;
}

 

接着是UISearchBarDelegate,UISearchDisplayDelegate的协议方法

-(BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
{
    NSLog(@"搜索开始");
    return YES;
}
-(BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar
{
    NSLog(@"搜索结束");
    return YES;
}
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
    NSPredicate *preicate=[NSPredicate predicateWithFormat:@"SELF CONTAINS[c] %@",searchString];
    if (self.resultArray!=nil) {
        [self.resultArray removeAllObjects];
    }
    self.resultArray=[NSMutableArray arrayWithArray:[self.dataArray filteredArrayUsingPredicate:preicate]];
    NSLog(@"%lu",(unsigned long)self.resultArray.count);
    return YES;
}

 

在这里我们用来区分的关键代码还是谓词,相对于正则表达式,谓词已经是很简单了.不过我还是记不住

运行结果如下:

 

虽然这个很好用,但是苹果公司在iOS8的时候已经讲他打上了红杠,推荐我们使用的是UISearchController

首先需要添加的协议有<UITableViewDataSource,UITableViewDelegate,UISearchBarDelegate,UISearchResultsUpdating>

还是现在xib文件中拖入一个tabView,为了不相互影响,我将两个放在不同的视图里

同样声明两个数组,功能同上;还有一个UIsearchController(因为我不知道拖的话是哪个控件,所以只好手写):

@property(nonatomic,retain)UISearchController *seachController;
@property(nonatomic,retain)NSMutableArray *resultArray;
@property(nonatomic,retain)NSMutableArray *dataArray;

 

初始化数组并手动创建searchController:

self.dataArray=[[NSMutableArray alloc]initWithObjects:@"张三",@"李四",@"王五",@"阿五",@"云飞",@"asd", nil];
    
    self.seachController=[[UISearchController alloc]initWithSearchResultsController:nil];
    self.seachController.searchResultsUpdater=self;
    self.seachController.dimsBackgroundDuringPresentation=NO;
    self.seachController.hidesNavigationBarDuringPresentation=NO;
    self.seachController.searchBar.frame=CGRectMake(self.seachController.searchBar.frame.origin.x, self.seachController.searchBar.frame.origin.y, self.seachController.searchBar.frame.size.width, 44.0);
    self.tableView.tableHeaderView=self.seachController.searchBar;

 

同样道理,这里也是有两个搜索结果,所以需要判断:

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (!self.seachController.active) {
        return self.dataArray.count;
    }
    else
    {
        return self.resultArray.count;
    }
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *str=@"cell";
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:str];
    if (cell==nil) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str];
    }
    if (!self.seachController.active) {
        cell.textLabel.text=self.dataArray[indexPath.row];
    }
    else
    {
        cell.textLabel.text=self.resultArray[indexPath.row];
    }
    return cell;
}

 

协议方法,在这里我们用的还是谓词判断

-(void)updateSearchResultsForSearchController:(UISearchController *)searchController
{
    NSString *searchString=self.seachController.searchBar.text;
    NSPredicate *preicate=[NSPredicate predicateWithFormat:@"SELF CONTAINS[c] %@",searchString];
    if (self.resultArray!=nil) {
        [self.resultArray removeAllObjects];
    }
    self.resultArray=[NSMutableArray arrayWithArray:[self.dataArray filteredArrayUsingPredicate:preicate]];
    NSLog(@"%lu",(unsigned long)self.resultArray.count);
    [self.tableView reloadData];
}

 

结果演示:

 

我写的随笔大部分是有源码的,如果有想要的,可以联系我.我的个人资料里有我的QQ.