如何用UIsearchController方法替换IOS 8中已弃用的SearchDisplayController?

7

我显然还不熟悉Xcode。在iOS 8中,SeacrhDisplayController已经被弃用了,而我不知道如何在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];

        }
    }

}
3个回答

5
我有相同的问题,我按照以下步骤操作: 步骤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;
}

步骤四: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:现在只需运行并检查


5

0
你有没有在苹果开发者网站上看过《Table Search with UISearchController (Obj-C and Swift)》的示例代码呢?

问题在于这个解决方案需要iOS 8,而我的应用程序仍支持iOS 7。是否有一些适用于所有系统的解决方案,至少在我放弃iOS 7之前? - Fabrizio Bartolomucci

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接