【问题标题】:How To Replace SearchDisplayController (Deprecated in IOS 8) by UIsearchController method如何通过 UIsearchController 方法替换 SearchDisplayController(在 IOS 8 中已弃用)
【发布时间】:2014-12-18 19:32:40
【问题描述】:

我显然还是 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];

        }
    }

}

【问题讨论】:

    标签: uisearchbar uisearchdisplaycontroller uisearchcontroller


    【解决方案1】:

    我知道这篇文章很旧,但我遇到了同样的问题,我找到了以下教程,比 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

    选择更适合您的需求,祝您编码愉快!

    干杯,

    【讨论】:

      【解决方案2】:

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

      第 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

      【讨论】:

        【解决方案3】:

        您是否查看过 Apple 开发者网站上的示例代码“使用 UISearchController(Obj-C 和 Swift)进行表格搜索”?

        【讨论】:

        • 问题是这个解决方案需要 iOS 8 并且我的应用仍然支持 iOS 7。是否有适用于所有系统的解决方案,至少在我放弃 iOS 7 之前?
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-11-15
        • 1970-01-01
        • 1970-01-01
        • 2015-05-01
        • 2016-03-21
        • 1970-01-01
        • 2013-09-25
        相关资源
        最近更新 更多