如何用UIsearchController方法替换SearchDisplayController(在IOS 8中不推荐使用)

时间:2014-12-18 19:32:41

标签: uisearchbar uisearchdisplaycontroller uisearchcontroller

我显然还是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 个答案:

答案 0 :(得分:3)

我知道这篇文章已经过时了,但我遇到了同样的问题而且我找到了以下教程,比Apple的例子容易得多:

  1. http://www.ioscreator.com/tutorials/add-search-table-view-tutorial-ios8-swift
  2. http://useyourloaf.com/blog/2015/02/16/updating-to-the-ios-8-search-controller.html
  3. 选择更适合您需求的产品,并获得快乐的编码!

    干杯,

答案 1 :(得分:0)

您是否检查了示例代码"使用UISearchController进行表搜索(Obj-C和Swift)"在Apple Developer网站上?

答案 2 :(得分:0)

我有同样的问题,请按照以下步骤操作:

步骤1:UISearchResultsUpdating添加到您的.h文件中

步骤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