0

我想在一个视图控制器(iPad)中放置两个搜索显示。我在视图控制器中拖动了两个搜索显示控制器,但是,只有一个搜索显示有效。

在 Connections Inspector 中,我发现一个搜索显示的出口“searchDisplayController”连接到“搜索显示控制器”,但另一个没有此连接。我认为这就是为什么只有一个搜索显示有效的原因。

我的问题是:我们如何在一个视图控制器中使用两个搜索显示?我想我的方法:拖动两个Search Display Controller可能不正确。

PS。我使用以下代码来确定关注哪个搜索显示。

- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {
    if (searchBar == self.customerTelSearchBar) {
        telSearchEditing = YES;
        addressSearchEditing = NO;
    }else if(searchBar == self.addressSearchBar){
        telSearchEditing = NO;
        addressSearchEditing = YES;
    }    
    return YES;
}
4

1 回答 1

1

总是不使用情节提要,但是当我使用两个搜索显示的编程实现时,它可以工作。我在这里发布我的代码:

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Init customerSearchDisplayController
    self.customerTelSearchBar.delegate = self;
    customerSearchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:self.customerTelSearchBar contentsController:self];
    customerSearchDisplayController.delegate = self;
    customerSearchDisplayController.searchResultsDataSource = self;
    customerSearchDisplayController.searchResultsDelegate = self;
    // Init addressSearchDisplayController
    self.addressSearchBar.delegate = self;
    addressSearchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:self.addressSearchBar contentsController:self];
    addressSearchDisplayController.delegate = self;
    addressSearchDisplayController.searchResultsDataSource = self;
    addressSearchDisplayController.searchResultsDelegate = self;
    // SearchBar status
    telSearchEditing = NO;
    addressSearchEditing = NO;
}

-(void)filterTelForSearchText:(NSString*)searchText {
    [filtredCustomersArray removeAllObjects];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.tel contains[c] %@",searchText];
    filtredCustomersArray = [NSMutableArray arrayWithArray:[allCustomersArray filteredArrayUsingPredicate:predicate]];
}

-(void)filterAddressForSearchText:(NSString*)searchText {
    [filtredAddressArray removeAllObjects];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.address contains[c] %@",searchText];
    filtredAddressArray = [NSMutableArray arrayWithArray:[allAddressArray filteredArrayUsingPredicate:predicate]];
}

-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
    if (telSearchEditing) {
        [self filterTelForSearchText:searchString];
    }else if (addressSearchEditing){
        [self filterAddressForSearchText:searchString];
    }
        return YES;
}

- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {
    if (searchBar == self.customerTelSearchBar) {
        telSearchEditing = YES;
        addressSearchEditing = NO;
    }else if(searchBar == self.addressSearchBar){
        telSearchEditing = NO;
        addressSearchEditing = YES;
    }
    return YES;
}
于 2013-09-10T14:50:55.643 回答