gpt4 book ai didi

ios - IOS8.0 UISearchController使用方法

转载 作者:行者123 更新时间:2023-11-28 21:41:01 25 4
gpt4 key购买 nike

UISearchDisplayController 在 IOS8.0 中被弃用

现在想用UISearchController实现一个位置搜索APP

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

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

该应用程序每次在

中获得搜索建议结果
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText

然后我将结果存储到 ResultArray。但是如何在屏幕上显示结果呢?我给searchResultsController.view属性赋值tableview后,结果列表还是显示不出来

非常感谢

最佳答案

@property (strong, nonatomic) UISearchController *searchController;

现在我们可以在 viewDidLoad 方法中创建和设置搜索 Controller 。

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;

接下来我们将搜索栏 View 添加到 TableView 标题中:

self.tableView.tableHeaderView = self.searchController.searchBar;

[self.searchController.searchBar sizeToFit];

UISearchResultsUpdating 委托(delegate)

配置搜索 Controller 后,剩下的大部分是样板代码。我们需要实现 UISearchResultsUpdating 委托(delegate),以便在搜索文本发生变化时生成新的过滤结果:

- (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];
}

还有你手杖.. Learn From here

enter image description here

另一种方式我已经添加了Another Solution,看看可能对你有用。

首先将 UISearchDisplayController 添加到您的 TableView ,然后设置其委托(delegate)。实现以下方法。

Demo Project

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];
}

关于ios - IOS8.0 UISearchController使用方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32064450/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com