在IOS8.0中不推荐使用UISearchDisplayController
现在我想使用UISearchController来实现位置搜索APP
我想在用户在搜索栏中输入一些单词后实现搜索建议列表。
到目前为止,我已经完成了服务器的建议结果,但我不知道如何显示它。
该应用每次都会在
中获得搜索建议结果-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
我将结果存储到ResultArray中。 但是如何在屏幕上显示结果?将表视图分配给 searchResultsController.view 属性后,结果列表仍然无法显示
非常感谢
答案 0 :(得分:2)
@property (strong, nonatomic) UISearchController *searchController;
现在,我们可以在viewDidLoad方法中创建和设置搜索控制器。
self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
self.searchController.searchResultsUpdater = self;
self.searchController.dimsBackgroundDuringPresentation = NO;
self.searchController.searchBar.scopeButtonTitles = @[NSLocalizedString(@"ScopeButtonCountry",@"Country"),
NSLocalizedString(@"ScopeButtonCapital",@"Capital")];
self.searchController.searchBar.delegate = self;
接下来,我们将搜索栏视图添加到表格视图标题中:
self.tableView.tableHeaderView = self.searchController.searchBar;
[self.searchController.searchBar sizeToFit];
UISearchResultsUpdating委托
配置搜索控制器后,其余部分主要是样板代码。我们需要实现UISearchResultsUpdating委托,以便在搜索文本发生变化时生成新的过滤结果:
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController
{
NSString *searchString = searchController.searchBar.text;
[self searchForText:searchString scope:searchController.searchBar.selectedScopeButtonIndex];
[self.tableView reloadData];
}
UISearchBarDelegate - 范围栏
- (void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope
{
[self updateSearchResultsForSearchController:self.searchController];
}
AND FOre MOre YOu cane .. Learn From here
首先将UISearchDisplayController添加到表视图然后设置其委托。 实现以下方法。
In your .h File
@interface ViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {
NSMutableArray *contentList;
NSMutableArray *filteredContentList;
BOOL isSearching;
}
@property (strong, nonatomic) IBOutlet UITableView *tblContentList;
@property (strong, nonatomic) IBOutlet UISearchBar *searchBar;
@property (strong, nonatomic) IBOutlet UISearchDisplayController *searchBarController;
In your .m File
Filling the sample data (Optional Only For Demo Purpose)
- (void)viewDidLoad {
[super viewDidLoad];
contentList = [[NSMutableArray alloc] initWithObjects:@"iPhone", @"iPod", @"iPod touch", @"iMac", @"Mac Pro", @"iBook",@"MacBook", @"MacBook Pro", @"PowerBook", nil];
filteredContentList = [[NSMutableArray alloc] init];
}
Now implement the Table View Delegate and Datasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
if (isSearching) {
return [filteredContentList count];
}
else {
return [contentList count];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell...
if (isSearching) {
cell.textLabel.text = [filteredContentList objectAtIndex:indexPath.row];
}
else {
cell.textLabel.text = [contentList objectAtIndex:indexPath.row];
}
return cell;
}
Search Function Responsible For Searching
- (void)searchTableList {
NSString *searchString = searchBar.text;
for (NSString *tempStr in contentList) {
NSComparisonResult result = [tempStr compare:searchString options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0, [searchString length])];
if (result == NSOrderedSame) {
[filteredContentList addObject:tempStr];
}
}
}
Search Bar Implementation
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
isSearching = YES;
}
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
NSLog(@"Text change - %d",isSearching);
//Remove all objects first.
[filteredContentList removeAllObjects];
if([searchText length] != 0) {
isSearching = YES;
[self searchTableList];
}
else {
isSearching = NO;
}
// [self.tblContentList reloadData];
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
NSLog(@"Cancel clicked");
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
NSLog(@"Search Clicked");
[self searchTableList];
}