如何使用 uisearchcontroller 和 objectiveC 在 ios 9 中添加表视图的搜索选项
How to add the search option for a tableview in ios 9 using uisearchcontroller, with objectiveC
我有一个显示数据成功的tableView,现在我想要的是为它提供搜索功能。 UISearchDisplayController 在 iOS 9 中被弃用,我是 iOS 的新手。所以请告诉我这样做的方法。
如果有人可以逐步提供代码,我将不胜感激,这也会有所帮助 others.This 是我的 tableView 代码。
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [airportList count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ci"];
Details *newDetails = [airportList objectAtIndex:indexPath.row];
cell.textLabel.text = newDetails.airport;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
Details *newDetails = [airportList objectAtIndex:indexPath.row];
NSString *selectedText = newDetails.airport;
[[NSUserDefaults standardUserDefaults] setObject:selectedText forKey:@"st"];
[[NSUserDefaults standardUserDefaults] synchronize];
[self dismissViewControllerAnimated:YES completion:nil];
}
您可以通过Apple Sample Guide学习:Table Search with UISearchController.
“Table Search with UISearchController”
是一个 iOS 示例应用程序,它演示了如何使用 UISearchController
。搜索控制器管理搜索栏的呈现(与结果视图控制器的内容一致)。
您可以在 iOS 9
中使用 UISearchController
首先为UISearchController
声明一个属性
@property (strong, nonatomic) UISearchController *searchController;
然后,在viewDidLoad
self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
self.searchController.searchResultsUpdater = self;
self.searchController.dimsBackgroundDuringPresentation = NO;
self.searchController.searchBar.delegate = self;
创建 UISearchController
时,我们不需要单独的搜索结果控制器,因为我们将使用 UITableViewController
本身。
同样,我们还将使用 UITableViewController
通过实现 UISearchResultsUpdating
协议来更新搜索结果。
我们不想让底层内容变暗,因为我们希望在用户输入搜索栏时显示过滤后的结果。
UISearchController
负责为我们创建搜索栏。
当用户更改搜索范围时,UITableViewController
还将充当搜索栏代理。
接下来,将 searchBar
添加到 table 查看 header
self.tableView.tableHeaderView = self.searchController.searchBar;
由于搜索视图在活动时覆盖 table 视图,因此我们让 UITableViewController
定义表示上下文:
self.definesPresentationContext = YES;
我们需要实施 UISearchResultsUpdating
委托,以便在搜索文本发生变化时生成新的过滤结果:
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController
{
NSString *searchString = searchController.searchBar.text;
[self searchForText:searchString scope:searchController.searchBar.selectedScopeButtonIndex];
[self.tableView reloadData];
}
我有一个显示数据成功的tableView,现在我想要的是为它提供搜索功能。 UISearchDisplayController 在 iOS 9 中被弃用,我是 iOS 的新手。所以请告诉我这样做的方法。 如果有人可以逐步提供代码,我将不胜感激,这也会有所帮助 others.This 是我的 tableView 代码。
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [airportList count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ci"];
Details *newDetails = [airportList objectAtIndex:indexPath.row];
cell.textLabel.text = newDetails.airport;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
Details *newDetails = [airportList objectAtIndex:indexPath.row];
NSString *selectedText = newDetails.airport;
[[NSUserDefaults standardUserDefaults] setObject:selectedText forKey:@"st"];
[[NSUserDefaults standardUserDefaults] synchronize];
[self dismissViewControllerAnimated:YES completion:nil];
}
您可以通过Apple Sample Guide学习:Table Search with UISearchController.
“Table Search with UISearchController”
是一个 iOS 示例应用程序,它演示了如何使用 UISearchController
。搜索控制器管理搜索栏的呈现(与结果视图控制器的内容一致)。
您可以在 iOS 9
中使用UISearchController
首先为UISearchController
@property (strong, nonatomic) UISearchController *searchController;
然后,在viewDidLoad
self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
self.searchController.searchResultsUpdater = self;
self.searchController.dimsBackgroundDuringPresentation = NO;
self.searchController.searchBar.delegate = self;
创建 UISearchController
时,我们不需要单独的搜索结果控制器,因为我们将使用 UITableViewController
本身。
同样,我们还将使用 UITableViewController
通过实现 UISearchResultsUpdating
协议来更新搜索结果。
我们不想让底层内容变暗,因为我们希望在用户输入搜索栏时显示过滤后的结果。
UISearchController
负责为我们创建搜索栏。
当用户更改搜索范围时,UITableViewController
还将充当搜索栏代理。
接下来,将 searchBar
添加到 table 查看 header
self.tableView.tableHeaderView = self.searchController.searchBar;
由于搜索视图在活动时覆盖 table 视图,因此我们让 UITableViewController
定义表示上下文:
self.definesPresentationContext = YES;
我们需要实施 UISearchResultsUpdating
委托,以便在搜索文本发生变化时生成新的过滤结果:
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController
{
NSString *searchString = searchController.searchBar.text;
[self searchForText:searchString scope:searchController.searchBar.selectedScopeButtonIndex];
[self.tableView reloadData];
}