UISearchBar

UISearchBar的委托协议是UISearchBarDelegate,不需要数据源协议
UISearchBar_第1张图片
显示效果

UISearchBar_第2张图片
显示效果

源代码

#import "ViewController.h"

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

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    //设置搜索栏委托对象为当前控制器
    self.searchBar.delegate = self;
    //设置中文/英文搜索栏ScopeBar为隐藏
    self.searchBar.showsScopeBar = NO;
    [self.searchBar sizeToFit];
    
    NSBundle *bundle = [NSBundle mainBundle];
    NSString *plistPath = [bundle pathForResource:@"team" ofType:@"plist"];
    //获取plist文件中的全部球队信息
    self.listTeams = [NSArray arrayWithContentsOfFile:plistPath];
    
    //初次进入查询所有的数据
    [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
//获得焦点,成为第一响应者
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
{
    self.searchBar.showsScopeBar = true;
    [self.searchBar sizeToFit];
    return YES;
}

//点击键盘上的搜索按钮
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    self.searchBar.showsScopeBar = NO;
    [self.searchBar sizeToFit];
    [self.searchBar resignFirstResponder];
}

//点击搜索栏取消按钮
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
    //查询所有
    [self filterContentForSearchText:self.searchBar.text scope:-1];
    self.searchBar.showsScopeBar = NO;
    [self.searchBar sizeToFit];
    [self.searchBar resignFirstResponder];
}

//当文本内容发生改变时候调用
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    [self filterContentForSearchText:self.searchBar.text scope:self.searchBar.selectedScopeButtonIndex];
    [self.tableView reloadData];
}

//当搜索范围选择发生变化时候调用
- (void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope
{
    [self filterContentForSearchText:self.searchBar.text scope:self.searchBar.selectedScopeButtonIndex];
    [self.tableView reloadData];
}

@end

你可能感兴趣的:(UISearchBar)