我显然还是Xcode的新手。所以SeacrhDisplayController
在iOS 8中已弃用,我不知道如何在tableview上实现UIsearchController
。
我已经用谷歌搜索了,但是我没有看到任何特别的或清晰的教程。
这是我的SearchDisplayController
的代码:
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller
shouldReloadTableForSearchString:(NSString *)searchString
{
[self filterContentForSearchText:searchString
scope:[[self.searchDisplayController.searchBar scopeButtonTitles]
objectAtIndex:[self.searchDisplayController.searchBar
selectedScopeButtonIndex]]];
return YES;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView == self.searchDisplayController.searchResultsTableView) {
return [searchResults count];
} else {
return [categories count];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"FirstTableCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
if (tableView == self.searchDisplayController.searchResultsTableView) {
cell.textLabel.text = [searchResults objectAtIndex:indexPath.row];
} else {
cell.textLabel.text = [categories objectAtIndex:indexPath.row];
}
cell.imageView.image = [UIImage imageNamed:[thumbnails objectAtIndex:indexPath.row]];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableView == self.searchDisplayController.searchResultsTableView) {
[self performSegueWithIdentifier: @"showArrayDetail" sender: self];
}
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"showArrayDetail"]) {
SecondViewController *destViewController = segue.destinationViewController;
NSIndexPath *indexPath = nil;
if ([self.searchDisplayController isActive]) {
indexPath = [self.searchDisplayController.searchResultsTableView indexPathForSelectedRow];
destViewController.categoryName = [searchResults objectAtIndex:indexPath.row];
} else {
indexPath = [self.tableView1 indexPathForSelectedRow];
destViewController.categoryName = [categories objectAtIndex:indexPath.row];
}
}
}
最佳答案
我也有同样的问题,请按照以下步骤操作:
步骤1:
在您的.h文件中添加UISearchResultsUpdating
步骤2:
创建UISearchController
的属性
@property(nonatomic, strong) IBOutlet UISearchController *searchController
步骤3:
将代码添加到viewDidLoad()
- (void)viewDidLoad
{
NSLog(@"viewDidLoad Starts");
[super viewDidLoad];
self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
self.searchController.searchResultsUpdater = self;
self.searchController.dimsBackgroundDuringPresentation = false;
self.tableView.tableHeaderView = self.searchController.searchBar;
}
步骤4:
在
updateSearchResultsForSearchController
中隐含您的过滤器内容- (void)updateSearchResultsForSearchController:(UISearchController *)searchController {
// do your Filter code and reload tableView
[self.tableView reloadData];
}
步骤5:在
numberOfRowsInSection
上更改您的条件if (self.searchController.isActive) {
return [searchResults count];
} else {
return [categories count];
}
步骤6:现在只需
run and check
关于uisearchbar - 如何用UIsearchController方法替换SearchDisplayController(在iOS 8中已弃用),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27554280/