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
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

  • 安裝與依賴請參考github地址https://github.com/react-native-community...
    IPFK閱讀 651評論 0 0
  • 包括四個會話 簡單會話-通過NSURLSession靜態(tài)方法+sharedSession獲得NSURLSessio...
    IPFK閱讀 523評論 0 0
  • 在iOS 7中,一個重大的改變就是隨處可見的虛化,這在通知中心和控制中心表現得尤為搶眼: 然而,當開發(fā)者們著手去將...
    ch32053閱讀 2,662評論 2 28
  • NinePatch是一種很有用的PNG圖片格式,它可以在特定區(qū)域隨文字大小進行縮放。如下: 從上圖可以看到,背景圖...
    Ten_Minutes閱讀 1,353評論 0 2
  • 我于偶然在朋友的微信里看見簡書,竟莫名的有點激動,想來每天可以這樣記錄一下所謂的生活多模式狀態(tài),也算在平淡重復的時...
    夏_如風閱讀 193評論 1 1

友情鏈接更多精彩內容