Cha*_*era 1 objective-c ios uisearchcontroller
我有一个tableView成功显示数据,现在我想要的是为它提供搜索功能.UISearchDisplayController在iOS 9中已弃用,我是iOS新手.所以请告诉我这样做的方法.如果有人可以一步一步地提供代码,我很感激它,它也会帮助其他人.这是我的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];
}
Run Code Online (Sandbox Code Playgroud)
小智 21
您可以UISearchController在iOS 9中使用
首先声明一个属性 UISearchController
@property (strong, nonatomic) UISearchController *searchController;
Run Code Online (Sandbox Code Playgroud)
然后,在 viewDidLoad
self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
self.searchController.searchResultsUpdater = self;
self.searchController.dimsBackgroundDuringPresentation = NO;
self.searchController.searchBar.delegate = self;
Run Code Online (Sandbox Code Playgroud)
在创建时,UISearchController我们不需要单独的搜索结果控制器,因为我们将使用它UITableViewController自己.同样,我们也将使用UITableViewController它来实现UISearchResultsUpdating协议来更新搜索结果.我们不希望调暗基础内容,因为我们希望在用户键入搜索栏时显示过滤结果.该UISearchController负责创建我们的搜索栏的照顾.该UITableViewController也将作为搜索栏委托当用户改变搜索范围为.
接下来,添加searchBar到tableview标头
self.tableView.tableHeaderView = self.searchController.searchBar;
Run Code Online (Sandbox Code Playgroud)
由于搜索视图在活动时覆盖了表视图,因此我们UITableViewController定义了表示上下文:
self.definesPresentationContext = YES;
Run Code Online (Sandbox Code Playgroud)
我们需要实现UISearchResultsUpdating委托,以便在搜索文本发生变化时生成新的过滤结果:
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController
{
NSString *searchString = searchController.searchBar.text;
[self searchForText:searchString scope:searchController.searchBar.selectedScopeButtonIndex];
[self.tableView reloadData];
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9829 次 |
| 最近记录: |