UISearchBar

UISearchBar的委托協(xié)議是UISearchBarDelegate,不需要數(shù)據(jù)源協(xié)議
顯示效果

顯示效果

源代碼

#import "ViewController.h"

@interface ViewController ()<UISearchBarDelegate>
@property (weak, nonatomic) IBOutlet UISearchBar *searchBar;
/** 所有球隊(duì)信息 */
@property (nonatomic, strong) NSArray *listTeams;
/** 搜索后的球隊(duì)信息 */
@property (nonatomic, strong) NSMutableArray *listFilterTeams;
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    //設(shè)置搜索欄委托對(duì)象為當(dāng)前控制器
    self.searchBar.delegate = self;
    //設(shè)置中文/英文搜索欄ScopeBar為隱藏
    self.searchBar.showsScopeBar = NO;
    [self.searchBar sizeToFit];
    
    NSBundle *bundle = [NSBundle mainBundle];
    NSString *plistPath = [bundle pathForResource:@"team" ofType:@"plist"];
    //獲取plist文件中的全部球隊(duì)信息
    self.listTeams = [NSArray arrayWithContentsOfFile:plistPath];
    
    //初次進(jìn)入查詢所有的數(shù)據(jù)
    [self filterContentForSearchText:@"" scope:-1];
}

- (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: //英文 image字段保存英文名
            scopePredicate = [NSPredicate predicateWithFormat:@"SELF.image contains[c] %@", searchText];
            tempArray = [self.listTeams filteredArrayUsingPredicate:scopePredicate];
            self.listFilterTeams = [NSMutableArray arrayWithArray:tempArray];
            break;
        case 1:
            scopePredicate = [NSPredicate predicateWithFormat:@"SELF.name 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
{
    static NSString *CellIdentifier = @"CellIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }
    
    NSUInteger row = indexPath.row;
    NSDictionary *rowDict = [self.listFilterTeams objectAtIndex:row];
    cell.textLabel.text = [rowDict objectForKey:@"name"];
    cell.detailTextLabel.text = [rowDict objectForKey:@"image"];
    
    NSString *imagePath = [rowDict objectForKey:@"image"];
    imagePath = [imagePath stringByAppendingString:@".png"];
    cell.imageView.image = [UIImage imageNamed:imagePath];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    return cell;
}

#pragma mark - UISearchBarDelegate
//獲得焦點(diǎn),成為第一響應(yīng)者
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
{
    self.searchBar.showsScopeBar = true;
    [self.searchBar sizeToFit];
    return YES;
}

//點(diǎn)擊鍵盤上的搜索按鈕
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    self.searchBar.showsScopeBar = NO;
    [self.searchBar sizeToFit];
    [self.searchBar resignFirstResponder];
}

//點(diǎn)擊搜索欄取消按鈕
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
    //查詢所有
    [self filterContentForSearchText:self.searchBar.text scope:-1];
    self.searchBar.showsScopeBar = NO;
    [self.searchBar sizeToFit];
    [self.searchBar resignFirstResponder];
}

//當(dāng)文本內(nèi)容發(fā)生改變時(shí)候調(diào)用
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    [self filterContentForSearchText:self.searchBar.text scope:self.searchBar.selectedScopeButtonIndex];
    [self.tableView reloadData];
}

//當(dāng)搜索范圍選擇發(fā)生變化時(shí)候調(diào)用
- (void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope
{
    [self filterContentForSearchText:self.searchBar.text scope:self.searchBar.selectedScopeButtonIndex];
    [self.tableView reloadData];
}

@end

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容