0

我用 UITableView 和 UISearchDisplayController 创建了一个 UIViewController,问题是 shouldReloadTableForSearchString 没有运行……这是我的代码,你的错误在哪里?

- (void)viewDidLoad {

    [super viewDidLoad];

    array = [[NSMutableArray alloc] initWithObjects:@"ciao",@"mandi",@"viva",@"blabla", nil];
    arrayFiltrato = [[NSMutableArray alloc] initWithObjects:@"ciao",@"mandi",@"viva",@"blabla", nil];

    //[self.tableView setContentOffset:CGPointMake(0, 44.f) animated:NO];
    [self.tableView reloadData];

    [self setSearchBar];

}

- (void) viewWillAppear:(BOOL)animated {

    [super viewWillAppear:YES];
    [self.navigationController setNavigationBarHidden: NO];
}


- (void) setSearchBar {

    self.searchBar = [[UISearchBar alloc] init];
    [self.searchBar setAutocapitalizationType:UITextAutocapitalizationTypeNone];
    [self.searchBar setPlaceholder:@"Type a search term" ];
    [self.searchBar setTintColor:[UIColor blackColor]];
    [self.searchBar setDelegate:self];
    [self.searchBar sizeToFit];
    [self.tableView setTableHeaderView:self.searchBar];

    self.searchDisplay = [[UISearchDisplayController alloc] initWithSearchBar:self.searchBar contentsController:self];
    [self.searchDisplay setSearchResultsDataSource:self];
    [self.searchDisplay setSearchResultsDelegate:self];
    [self.searchDisplay setDelegate:self];
}


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)myTableView {

    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)myTableView numberOfRowsInSection:(NSInteger)section {

    if (myTableView == self.tableView) {
      return array.count;
    }
    else{
        return arrayFiltrato.count;
    }

}

- (UITableViewCell *)tableView:(UITableView *)myTableView cellForRowAtIndexPath:  (NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }

    if (myTableView == self.tableView) {
        cell.textLabel.text = [array objectAtIndex:indexPath.row];
    } else {
        cell.textLabel.text = [arrayFiltrato objectAtIndex:indexPath.row];
    }


    return cell;
}




#pragma mark -
#pragma mark Content Filtering

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope{

    [arrayFiltrato removeAllObjects];

    // if ([scope isEqualToString:@"All"] || [product.type isEqualToString:scope]) {

    for (NSString *str in array) {

        NSLog(@"str: %@ - scope: %@",str,scope);

        if ([scope isEqualToString:str]) {
            NSComparisonResult result = [str compare:searchText
                                             options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch)
                                               range:NSMakeRange(0, [searchText length])];
            if (result == NSOrderedSame) {
                [arrayFiltrato addObject:str];
            }
        }
    }
}

#pragma mark -
#pragma mark UIsearchDisplay Delegate Methods

- (BOOL)searchDisplay:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString{

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


    NSLog(@"ok");

    return YES;
}

- (BOOL)searchDisplay:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption{

    NSLog(@"ok");

    [self filterContentForSearchText:[self.searchDisplayController.searchBar text]
                               scope:[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:searchOption]];


    return YES;
}

- (void)searchDisplayDidBeginSearch:(UISearchDisplayController *)controller{

    [self.searchDisplayController.searchResultsTableView setDelegate:self];
}

- (void)searchDisplayDidEndSearch:(UISearchDisplayController *)controller{

    //[self.tableView setContentOffset:CGPointMake(0, 44.f) animated:YES];
}

#pragma mark -

-(void)searchBar:(id)sender{
    [self.searchDisplayController setActive:YES animated:YES];
}
4

1 回答 1

0

The problem is that the delegate method is wrong, isn't this:

- (BOOL)searchDisplay:(UISearchDisplayController *)controller shouldRelo....

but this:

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldRelo...
于 2013-10-07T06:53:09.167 回答