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 

Regards,

回答

1

最後,我自己解決了這個問題。

爲了解決這個問題,我在textDidChange中調用了shouldReloadTableForSearchString,然後增量搜索工作。一個差異初級講座:

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:

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

它將被ARC得到釋放,也不會調用任何委託方法,當你調用self.searchDisplayController(該UIViewController的屬性)這將是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

相關問題