2

단어의 증분 검색을 구현하고 싶습니다. 아래와 같이 구현했지만 filterContentForSearchText 및 shouldReloadTableForSearchString 메서드가 호출되지 않습니다. 왜?filterContentForSearchText 및 shouldReloadTableForSearchString이 호출되지 않음 (UITableView 및 UISearchBar)

WordsIndexViewController.h

#import <UIKit/UIKit.h> 

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

@end 

WordsIndexViewController.m

#import "WordsIndexViewController.h" 
#import "WordsShowViewController.h" 
#import "Word.h" 

@interface WordsIndexViewController() 

@property (strong, nonatomic) UITableView *tableView; 

@property (strong, nonatomic) NSArray *words; 
@property (strong, nonatomic) NSMutableArray *searchedWords; 

@end 

@implementation WordsIndexViewController 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view. 

    _words = [[app models] objectForKey:@"words"]; 
    _searchedWords = [NSMutableArray arrayWithArray:@[]]; 

    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 480) style:UITableViewStylePlain]; 
    tableView.delegate = self; 
    tableView.dataSource = self; 
    _tableView = tableView; 

    UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 44)]; 
    searchBar.delegate = self; // needed? 
    [searchBar sizeToFit]; // needed? 

    UISearchDisplayController *searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self]; 
    searchDisplayController.delegate = self; 
    searchDisplayController.searchResultsDelegate = self; 
    searchDisplayController.searchResultsDataSource = self; 
    _tableView.tableHeaderView = searchBar; 

    [self.view addSubview:_tableView]; 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

#pragma mark - related to UITableView 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    NSLog(@"START numberOfRowsInSection"); 

    NSArray *words = nil; 
    if (tableView == self.searchDisplayController.searchResultsTableView) { 
     words = _searchedWords; 
    } else { 
     words = _words; 
    } 

    return [words count]; 
} 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    NSLog(@"START cellForRowAtIndexPath"); 

    NSArray *words = nil; 
    if (tableView == self.searchDisplayController.searchResultsTableView) { 
     words = _searchedWords; 
    } else { 
     words = _words; 
    } 

    UITableViewCell *cell = [[UITableViewCell alloc] init]; 
    Word *word = words[indexPath.row]; 
    cell.textLabel.text = word.spelling; 
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 

    return cell; 
} 
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    NSArray *words = nil; 
    if (tableView == self.searchDisplayController.searchResultsTableView) { 
     words = _searchedWords; 
    } else { 
     words = _words; 
    } 

    WordsShowViewController *controller = [[WordsShowViewController alloc] initWithNibName:@"ViewController" bundle:nil]; 
    controller.word = (Word *)words[indexPath.row]; 
    [self.navigationController pushViewController:controller animated:YES]; 
} 

#pragma mark - related to Search Bar 
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString { 
    NSLog(@"Query: %@", searchString); // NOT CALLED... 

    [self filterContentForSearchText:searchString 
     scope:[ 
      [self.searchDisplayController.searchBar scopeButtonTitles] 
      objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex] 
     ] 
    ]; 

    return YES; 
} 
- (void)filterContentForSearchText:(NSString*)searchString scope:(NSString*)scope { 
    NSLog(@"START filterContentForSearchText"); // NOT CALLED... 

    [_searchedWords removeAllObjects]; 

    for(Word *word in _words) { 
     NSRange range = [word.spelling rangeOfString:searchString options:NSCaseInsensitiveSearch]; 
     if(range.length > 0) { 
      [_searchedWords addObject:word]; 
     } 
    } 
} 

@end 

Word.h

#import <Foundation/Foundation.h> 

@interface Word : NSObject 

@property (strong, nonatomic) NSString *identifier; 
@property (strong, nonatomic) NSString *spelling; 

- (id)initWith:(NSString *)spelling; 

@end 

Word.m

#import "Word.h" 

@implementation Word 

- (id)initWith:(NSString *)spelling { 
    self = [super init]; 
    if (!self) { 
     return nil; 
    } 

    CFUUIDRef uuid = CFUUIDCreate(NULL); 
    _identifier = (NSString *)CFBridgingRelease(CFUUIDCreateString(NULL, uuid)); 
    CFRelease(uuid); 

    _spelling = spelling; 

    return self; 
} 

@end 
,

감사합니다.

답변

1

마지막으로, 직접 질문을 해결했습니다.

이를 해결하기 위해 textDidChange에서 shouldReloadTableForSearchString을 호출 한 다음 증분 검색을 수행했습니다. DIFF는 belows :

WordsIndexViewController.m

@@ -16,6 +16,7 @@ 
     @property (strong, nonatomic) NSArray *words; 
@property (strong, nonatomic) NSMutableArray *searchedWords; 
[email protected] (strong, nonatomic) UISearchDisplayController *displayController; 

@end 

@@ -52,6 +53,7 @@ 
    searchDisplayController.searchResultsDelegate = self; 
    searchDisplayController.searchResultsDataSource = self; 
    _tableView.tableHeaderView = searchBar; 
+ _displayController = searchDisplayController; 

    [self.view addSubview:_tableView]; 
} 
@@ -132,5 +134,9 @@ 
     } 
    } 
} 
+- (void) searchBar:(UISearchBar *)searchBar textDidChange:(NSString *) searchText { 
+ [self searchDisplayController:_displayController shouldReloadTableForSearchString:searchText]; 
+} 

@end 

감사합니다,

+0

나를 위해 작동하지 않음 – Gank

1

는 헤더 파일에 UISearchDisplayController에 대한 바르를 작성해야합니다.

당신이 사용 UISearchDisplayController 작성하는 경우 : 당신이 self.searchDisplayController합니다 (있는 UIViewController의 재산을)를 호출 할 수 있습니다 때

UISearchDisplayController* searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchField contentsController:self];

가 ARC 발표 얻을 것이다

, 그것은 어떤 대리자 메서드를 호출하지 않습니다를이 nil 될 것입니다 .

그래서 수정은 다음과 같습니다 헤더 (.h) 파일 :

@interface MenuViewController : UIViewController <UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate, UISearchDisplayDelegate> { 
     UISearchDisplayController* searchDisplayController; 
     UISearchBar *searchField; 
     UITableView* tableView; 
     NSArray* searchResults; 
} 

및 구현 (하는 .m) 파일

:

searchField = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 49)]; 
searchField.delegate = self; 

searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchField contentsController:self]; 
searchDisplayController.delegate = self; 
searchDisplayController.searchResultsDataSource = self; 
searchDisplayController.searchResultsDelegate = self; 

tableView.tableHeaderView = searchField; 
tableView.contentOffset = CGPointMake(0, searchField.frame.size.height); 

그런 식으로 구현하면 나머지 코드에서 self.searchDisplayControllersearchDisplayController을 모두 호출 할 수 있습니다.