0

iOSで検索バーと検索ディスプレイコントローラーを使用することを学んでいます。テーブルビューから行を選択すると、新しいビューに行の名前のラベルが表示されます。いくつかの問題が発生しています。まず、検索バーにテキストを追加し始めて検索表示が表示されると、検索バーが表示されなくなります。フィルタリングされた結果が表示されますが、行を選択しても次のビューに移動しません。これは、テーブルがフィルタリングされている場合にのみ発生します。コードは次のとおりです。

ViewController.h

#import <UIKit/UIKit.h>
#import "DetailViewController.h"

@interface ViewController : UIViewController <UITableViewDelegate, UITableViewDataSource, UISearchDisplayDelegate, UISearchBarDelegate>

@property (strong, nonatomic) IBOutlet UISearchBar *searchBar;
@property (strong, nonatomic) IBOutlet UITableView *tableView;

@end

ViewController.m

#import "ViewController.h"

@interface ViewController ()

@property (nonatomic, strong) NSArray *objects;
@property (nonatomic, strong) NSArray *filteredObjects;

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    _objects = [[NSMutableArray alloc] initWithObjects:@"One", @"Two", @"Three", @"Four", @"Five", @"Six", @"Seven", @"Eight", @"Nine", @"Ten", nil];
    _tableView.delegate = self;
    _tableView.dataSource = self;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == self.searchDisplayController.searchResultsTableView) {
        return [_filteredObjects count];

    } else {
        return [_objects count];
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    if (tableView == self.searchDisplayController.searchResultsTableView) {
        cell.textLabel.text = [_filteredObjects objectAtIndex:indexPath.row];
    } else {
        cell.textLabel.text = [_objects objectAtIndex:indexPath.row];
    }

    return cell;
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"ToDetail"]) {
        DetailViewController *detailViewController = [segue destinationViewController];

        if (self.searchDisplayController.active) {
            NSIndexPath *indexPath = [self.searchDisplayController.searchResultsTableView indexPathForSelectedRow];
            detailViewController.detailString = [_filteredObjects objectAtIndex:indexPath.row];
        } else {
            NSIndexPath *indexPath = [_tableView indexPathForSelectedRow];
            detailViewController.detailString = [_objects objectAtIndex:indexPath.row];
        }

    }
}

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
    NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"SELF contains[c] %@", searchText];
    _filteredObjects = [_objects filteredArrayUsingPredicate:resultPredicate];
}

-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
    [self filterContentForSearchText:searchString
                               scope:[[self.searchDisplayController.searchBar scopeButtonTitles]
                                      objectAtIndex:[self.searchDisplayController.searchBar
                                                     selectedScopeButtonIndex]]];

    return YES;
}

助けてくれてありがとう!

4

1 に答える 1

0

行を選択すると、次のビューに移動しません。これは、テーブルがフィルター処理されている場合にのみ発生します

の実装がないためですtableView:didSelectRowAtIndexPath:

「フィルター処理されたテーブル ビュー」などというものは実際には存在しないことを覚えておいてください。2 つのテーブル ビューがあります。通常のビューと、検索ディスプレイ コントローラーによって表示されるビューです。これはのテーブル ビュー (基本的には自分のテーブルの前に表示される) であり、何をしたいのか、どのように表示したいのかを設定する必要があります。

于 2014-02-23T21:41:57.597 に答える