UISearchController+NSPredicate的簡單使用

#import "ViewController.h"

@interface ViewController ()  <UISearchBarDelegate, UISearchResultsUpdating>

@property (strong, nonatomic) UISearchController *searchController;

//全部数据
@property (nonatomic, strong) NSArray *listTeams;


//过滤后的数据
@property (nonatomic, strong) NSMutableArray *listFilterTeams;

//内容过滤方法
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSUInteger)scope;

@end




@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"team"
                                           ofType:@"plist"];
    //获取属性列表文件中的全部数据
    self.listTeams = [[NSArray alloc] initWithContentsOfFile:plistPath];
    
    //查询所有数据
    [self filterContentForSearchText:@"" scope:-1];
    
    //实例化UISearchController
    self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
    //设置self为更新搜索结果对象
    self.searchController.searchResultsUpdater = self;
    //在搜索是背景设置为灰色
    self.searchController.dimsBackgroundDuringPresentation = FALSE;
    
    //设置搜索范围栏中的按钮
    self.searchController.searchBar.scopeButtonTitles = @[@"名字", @"圖片"];
    self.searchController.searchBar.delegate = self;
    
    //将搜索栏放到表视图的表头中
    self.tableView.tableHeaderView = self.searchController.searchBar;
    
    [self.searchController.searchBar sizeToFit];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark --内容过滤方法
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSUInteger)scope {
    
    if([searchText length]==0) {
        //查询所有
        self.listFilterTeams = [NSMutableArray arrayWithArray:self.listTeams];
        return;
    }
    
    NSPredicate *scopePredicate;
    NSArray *tempArray ;
    
    switch (scope) {
        case 0://中文 name字段是中文名
            //SELF.name contains[c] %@是Predicate字符串,SELF.name是要查詢的對象的name字段,contains[c]是包含字符的意思
            scopePredicate = [NSPredicate predicateWithFormat:@"SELF.name contains[c] %@",searchText];
            tempArray =[self.listTeams filteredArrayUsingPredicate:scopePredicate];
            self.listFilterTeams = [NSMutableArray arrayWithArray:tempArray];
            break;
        case 1: //英文 image字段保存英文名
            scopePredicate = [NSPredicate predicateWithFormat:@"SELF.image contains[c] %@",searchText];
            tempArray =[self.listTeams filteredArrayUsingPredicate:scopePredicate];
            self.listFilterTeams = [NSMutableArray arrayWithArray:tempArray];
            break;
        default:
            //查询所有
            self.listFilterTeams = [NSMutableArray arrayWithArray:self.listTeams];
            break;
    }
}


#pragma mark --UITableViewDataSource 协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.listFilterTeams count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *) indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CellIdentifier" forIndexPath:indexPath];
    
    NSUInteger row = [indexPath row];
    
    NSDictionary *rowDict = self.listFilterTeams[row];
    cell.textLabel.text = rowDict[@"name"];
    cell.detailTextLabel.text = rowDict[@"image"];
    
    NSString *imagePath = [[NSString alloc] initWithFormat: @"%@.png", rowDict[@"image"]];
    cell.imageView.image = [UIImage imageNamed:imagePath];
    
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    return cell;
}
//當切換"名字"和"圖片"按鈕時調用
#pragma mark --实现UISearchBarDelegate协议方法
- (void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope {
    [self updateSearchResultsForSearchController:self.searchController];
}

#pragma mark --实现UISearchResultsUpdating协议方法
//當搜索欄變成第一響應者,並且內容被改變時調用
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController {
    NSString *searchString = searchController.searchBar.text;
    //查询
    [self filterContentForSearchText:searchString scope:searchController.searchBar.selectedScopeButtonIndex];
    [self.tableView reloadData];
}

@end

屏幕截圖如下:

图片.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 安裝與依賴請參考github地址https://github.com/react-native-community...
    IPFK阅读 622评论 0 0
  • 包括四個會話 簡單會話-通過NSURLSession靜態方法+sharedSession獲得NSURLSessio...
    IPFK阅读 407评论 0 0
  • 在iOS 7中,一个重大的改变就是随处可见的虚化,这在通知中心和控制中心表现得尤为抢眼: 然而,当开发者们着手去将...
    ch32053阅读 2,569评论 2 28
  • NinePatch是一种很有用的PNG图片格式,它可以在特定区域随文字大小进行缩放。如下: 从上图可以看到,背景图...
    Ten_Minutes阅读 1,283评论 0 2
  • 我于偶然在朋友的微信里看见简书,竟莫名的有点激动,想来每天可以这样记录一下所谓的生活多模式状态,也算在平淡重复的时...
    夏_如风阅读 117评论 1 1