如何使用UISearchController IOS8.0

如何使用UISearchController IOS8.0,ios,Ios,IOS8.0中不推荐使用UISearchDisplayController 现在我想使用UISearchController实现一个位置搜索应用程序 我想在用户在搜索栏中键入一些单词后实现搜索建议列表 到目前为止,我已经完成了来自服务器的建议结果,但我不知道如何显示它 该应用程序每次都会收到搜索建议结果 -(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText 我将结果存储到ResultArra

IOS8.0中不推荐使用UISearchDisplayController

现在我想使用UISearchController实现一个位置搜索应用程序

我想在用户在搜索栏中键入一些单词后实现搜索建议列表

到目前为止,我已经完成了来自服务器的建议结果,但我不知道如何显示它

该应用程序每次都会收到搜索建议结果

-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
我将结果存储到ResultArray中。 但是我如何在屏幕上显示结果呢?在我将tableview分配给searchResultsController.view属性后,仍然无法显示结果列表

非常感谢

@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];
UISearchResultsUpdated代表

配置了搜索控制器之后,剩下的大部分都是样板代码。我们需要实现UISearchResultsUpdated委托,以便在搜索文本更改时生成新的筛选结果:

- (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];
}
你越是拐杖。。

另一种方式我添加了另一种解决方案,看一看可能对你有用。 首先将UISearchDisplayController添加到表视图,然后设置其委托。 实现以下方法

.h文件中的

@界面ViewController:UIViewController{
NSMutableArray*内容列表;
NSMutableArray*filteredContentList;
布尔正在研究;
}
@属性(强,非原子)IBUITableView*tblContentList;
@属性(强,非原子)IBUISearchBar*搜索栏;
@属性(强,非原子)IBMOutlet UISearchDisplayController*searchBarController;
在你的.m文件中
填充样本数据(可选,仅用于演示)
-(无效)viewDidLoad{
[超级视图下载];
contentList=[[NSMutableArray alloc]initWithObjects:@“iPhone”@“iPod”@“iPod touch”@“iMac”@“Mac Pro”@“iBook”@“MacBook”@“MacBook Pro”@“PowerBook”,nil];
filteredContentList=[[NSMutableArray alloc]init];
}
现在实现表视图委托和数据源
-(NSInteger)表格视图中的节数:(UITableView*)表格视图{
//返回节数。
返回1;
}
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节{
//返回节中的行数。
如果(正在搜索){
返回[filteredContentList计数];
}
否则{
返回[内容列表计数];
}
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
静态NSString*CellIdentifier=@“Cell”;
UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
如果(单元格==nil){
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault重用标识符:CellIdentifier];
}
//配置单元格。。。
如果(正在搜索){
cell.textlab.text=[filteredContentList objectAtIndex:indexath.row];
}
否则{
cell.textLabel.text=[contentList objectAtIndex:indexath.row];
}
返回单元;
}
负责搜索的搜索功能
-(作废)可搜索列表{
NSString*searchString=searchBar.text;
for(内容列表中的NSString*tempStr){
NSComparisonResult=[tempStr比较:搜索字符串选项:(NSCaseInsensitiveSearch | NSDiacriticInsensitiveSearch)范围:NSMakeRange(0,[searchString长度]);
如果(结果==SensorDeredName){
[filteredContentList addObject:tempStr];
}
}
}
搜索栏实现
-(无效)searchBarTextDidBeginEditing:(UISearchBar*)搜索栏{
isSearching=是;
}
-(无效)搜索栏:(UISearchBar*)搜索栏文本更改:(NSString*)搜索文本{
NSLog(@“文本更改-%d”,正在搜索);
//首先删除所有对象。
[filteredContentList removeAllObjects];
如果([searchText length]!=0){
isSearching=是;
[自查表];
}
否则{
isSearching=否;
}
//[self.tblContentList reloadData];
}
-(无效)搜索栏取消按钮选中:(UISearchBar*)搜索栏{
NSLog(@“单击取消”);
}
-(无效)搜索栏搜索按钮选中:(UISearchBar*)搜索栏{
NSLog(@“已单击搜索”);
[自查表];
}

看起来这个答案中有一些是抄袭而来的,可能会给你一些信任(当然,除非那是你的网站)。一切都在互联网上,你可以搜索。我在这里帮助那些没有那么多天赋的人。他们需要帮助,我会的。如果你有任何问题,那就是你的问题。我没有任何网站。我不需要。如果你有时间和天赋,请在这里展示。你会像我们一样获得声誉。嗯@佐纳比。不要把它当回事。但我伤害了你,然后抱歉……一切都很好@mehul我们都非常感谢你和所有人的帮助。只要你从某个地方复制,就把它记下来,这只是一种常见的礼貌。@zonabi,r u IOS开发者?
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];
}