美文网首页
UISearchController+NSPredicate的簡

UISearchController+NSPredicate的簡

作者: IPFK | 来源:发表于2017-10-22 15:33 被阅读0次
#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

相关文章

  • UISearchController+NSPredicate的簡

    屏幕截圖如下:

  • 午間戲作

    簡書 簡者,簡單、簡約、簡練也。 書者,書寫之文字內容也。其內容錯雜彙纂,乃成書籍卷冊。 簡書:簡單、簡約、簡練之...

  • 簡簡單單的2018

    “肥”宅快樂年 頂著肝爆的風險,熬夜已成日常,隨著“再戰一回合”的想法一次又一次在我腦海中出現,我知道這是屬於我的...

  • 簡簡單單。

    想做一個熱愛生活的女子,希望醒來後你可以為我做哪怕一年一次的早餐。然後問著窗台花卉的芬芳,哼著歌為它們澆水。一番打...

  • 簡簡單單

    >櫻花飄落是秒速五釐米,不知道你可曾聽過? 三年的時間足夠一個人做什麼? 三年的時間能夠改變一個人嗎? 三年……究...

  • 簡單的孩子

    簡單的孩子 從不去奢求的太多 心簡單了這世界 就會簡單了許多 該忙碌時就忙碌 該歇息時就歇息 偶而品位簡單的茶 吃...

  • 6-1 雍也可使南面章

    子曰:「雍也可使南面。」仲弓問子桑伯子,子曰:「可也簡。」仲弓曰:「居敬而行簡,以臨其民,不亦可乎?居簡而行簡,無...

  • 2018-12-18 お月さんはなぜ怒ったのか?21

    おおタマネギの二つの円い穴にご注目。 なんてことはない!簡単、簡単、簡単符!おっと、間違いました。簡単、簡単、感嘆...

  • 中華楷書簡體歷史

    簡體字其實一早出現在中國古代,只有很小一部分簡體字爲現代所造。 簡体字指由楷體字簡化的漢字字體。隸書是篆書的簡化,...

  • Hello 簡書

    Hi, This a demo post of 簡書. 簡書 use Markdown as main synta...

网友评论

      本文标题:UISearchController+NSPredicate的簡

      本文链接:https://www.haomeiwen.com/subject/iroguxtx.html