搜索功能

摘要:

正文:

搜索框所在控制器.m

  • 协议与属性
1
2
3
4
5
6
@interface HZAutoPartsViewController ()<UITableViewDelegate,UITableViewDataSource,UISearchResultsUpdating, UISearchBarDelegate>
// 搜索控制器
@property (nonatomic, strong) UISearchController *searchController;
// 搜索结果
@property (nonatomic, strong) NSMutableArray *searchResults;
@end
  • viewDidLoad
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = HZWhiteColor;
/**
* 搜索相关
*/
self.searchResults = [NSMutableArray array];
HZTSearchResultController *searchResultVC = [[HZTSearchResultController alloc]init];
searchResultVC.title = self.title;

UINavigationController *searchResultNAV = [[UINavigationController alloc]initWithRootViewController:searchResultVC];
self.searchController = [[UISearchController alloc]initWithSearchResultsController:searchResultNAV];
// 设置代理
self.searchController.searchResultsUpdater = self;
// 搜索框frame
self.searchController.searchBar.frame = CGRectMake(self.searchController.searchBar.frame.origin.x, self.searchController.searchBar.frame.origin.y, self.searchController.searchBar.frame.size.width, 44.0);
//
self.definesPresentationContext = YES;
self.searchController.searchBar.placeholder = @"编码,名称,编码+名称";
self.searchController.searchBar.showsCancelButton = YES;
UIButton *cancelButton = [self.searchController.searchBar valueForKey:@"_cancelButton"];
[cancelButton setTitle:@"取消"forState:UIControlStateNormal];

_autoPartsTableView.tableHeaderView = self.searchController.searchBar;

}
  • 代理方法的实现
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#pragma mark - UISearchBarDelegate

// Workaround for bug: -updateSearchResultsForSearchController: is not called when scope buttons change
- (void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope {
[self updateSearchResultsForSearchController:self.searchController];
}
#pragma mark ** UISearchResultUpdating **
-(void)updateSearchResultsForSearchController:(UISearchController *)searchController{
NSString *searchString = [self.searchController.searchBar text];

[self updateFilteredContentForProductName:searchString];

if (self.searchController.searchResultsController) {
UINavigationController *navController = (UINavigationController *)self.searchController.searchResultsController;

HZTSearchResultController *vc = (HZTSearchResultController *)navController.topViewController;
vc.searchResults = self.searchResults;
[vc.tableView reloadData];
}

}


#pragma mark - Content Filtering
- (void)updateFilteredContentForProductName:(NSString *)productName{
// Update the filtered array based on the search text and scope.
if ((productName == nil) || [productName length] == 0) {
self.searchResults = [_dataArray mutableCopy];
return;
}


[self.searchResults removeAllObjects]; // First clear the filtered array.

/* Search the main list for products whose type matches the scope (if selected) and whose name matches searchText; add items that match to the filtered array.
*/
for (HZAutoPartsModel *model in _dataArray) {
NSUInteger searchOptions = NSCaseInsensitiveSearch | NSDiacriticInsensitiveSearch;
NSRange productNameRange = NSMakeRange(0, model.partsName.length);
NSRange foundRange = [model.partsName rangeOfString:productName options:searchOptions range:productNameRange];
if (foundRange.length > 0) {
[self.searchResults addObject:model];
}
}
}