uiarchconapolelr中的uisearchdisaplycontroller - 以编程方式

时间:2013-10-05 13:23:35

标签: uitableview uiviewcontroller uisearchbar uisearchdisplaycontroller

我创建了一个带有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];
}

1 个答案:

答案 0 :(得分:0)

问题是委托方法错了,不是这样的:

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

但是这个:

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