7

我显然还是 Xcode 的新手。所以SeacrhDisplayController在 iOS 8 中已弃用,我不知道如何UIsearchController在 tableview 上实现。

我已经用谷歌搜索了它,但我没有看到任何特定或明确的教程。

这是我的代码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];

        }
    }

}
4

3 回答 3

5

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

选择更适合您的需求并拥有愉快的编码!

干杯,

于 2015-06-06T09:27:03.217 回答
5

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

第 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

于 2018-10-26T08:28:08.827 回答
0

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

于 2015-01-07T17:51:06.273 回答