UISearchBar的委托協議是UISearchBarDelegate
,不需要數據源協議
顯示效果
顯示效果
源代碼
#import "ViewController.h"
@interface ViewController ()<UISearchBarDelegate>
@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