UISearchDisplayController搜索栏自动键入字母

时间:2014-08-08 05:18:05

标签: ios7 uisearchbar edit uisearchdisplaycontroller

在UISearchDisplayController中编辑搜索栏时,它会递归重复第一个键入的字母,直到按下第二个字母。

以下是发生的事情:

enter image description here

请建议任何可能的解决方案。 代码:

-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString 
{
    [self filterContent:searchString scope:
    [[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];
    return YES;
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)SearchBar
{
    [SearchBar resignFirstResponder];
    SearchBar.text = @"";
    [self.tableView reloadData];
}
- (void)searchDisplayControllerDidBeginSearch:(UISearchDisplayController *)controller
{
    controller.searchBar.showsCancelButton = YES;
}
-(void)filterContent:(NSString*)searchText scope:(NSString*)scope 
{
    _searchArray = [[NSArray alloc] init];
    NSPredicate *predicate = [NSCompoundPredicate orPredicateWithSubpredicates:[NSArray arrayWithObjects:[NSPredicate predicateWithFormat:@"SELF.user_name contains[c] %@", searchText], [NSPredicate predicateWithFormat:@"SELF.country_name contains[c] %@", searchText], [NSPredicate predicateWithFormat:@"SELF.city contains[c] %@", searchText], nil]];
   _searchArray = [_users filteredArrayUsingPredicate:predicate];
}

1 个答案:

答案 0 :(得分:1)

@JananiM我认为你在你的委托方法中分配的这一行[self filterContent:searchString ...]中存在问题。

试试这个

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
[self filterContentForSearchText:searchString
                           scope:[self.searchDisplayController.searchBar scopeButtonTitles][[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];
return YES;

}
 我已经在我的应用程序中实现了你的代码(你在上面发布了)。我没有得到这样的东西。我也检查了断点。检查这个

enter image description here

enter image description here

enter image description here

enter image description here