输入文本时,UISearchBar不显示任何结果

时间:2016-12-13 04:02:08

标签: ios objective-c

当我的用户搜索我的UISearchBar中的项目时,如果在栏中输入的完整单词与其中一个结果匹配,则会显示结果。 例如如果" Panda"输入后,Panda弹出tableView结果。但是,如果"潘"键入,没有显示结果。如何在用户输入时使搜索结果过滤器工作?即使只是" pan"也应该展示Panda。输入。

我的过滤器代码目前如下所示:

的.m

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    NSPredicate *resultPredicate = [NSPredicate
                                    predicateWithFormat:@"SELF contains[cd] %@",
                                    searchText];

    searchResults = [self.neighbourData filteredArrayUsingPredicate:resultPredicate];
}

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

    return YES;
} */


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

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

    if (tableView == self.searchDisplayController.searchResultsTableView) {
        return [searchResults count];

    } else { 

    return [self.neighbourData count];

    }
}

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

    static NSString *NetworkTableIdentifier = @"sidebarCell";

    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

    sidebarCell *cell = (sidebarCell *)[tableView dequeueReusableCellWithIdentifier:NetworkTableIdentifier];
    if (cell == nil)

    {



        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"sidebarCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }

    if (tableView == self.searchDisplayController.searchResultsTableView) {



        NSDictionary *userName = [searchResults objectAtIndex:indexPath.row];
        [[cell username] setText:[userName objectForKey:@"first name"]];

        NSDictionary *userlast = [searchResults objectAtIndex:indexPath.row];
        [[cell lastName] setText:[userlast objectForKey:@"last name"]];

        NSDictionary *userBio = [searchResults objectAtIndex:indexPath.row];
        [[cell userDescription] setText:[userBio objectForKey:@"userbio"]];



        NSString *profilePath = [[searchResults objectAtIndex:indexPath.row] objectForKey:@"photo_path"];

        [cell.usermini sd_setImageWithURL:[NSURL URLWithString:profilePath]];

        NSLog(@"This is profilePath %@",profilePath);



    } else {

    NSDictionary *userName = [self.neighbourData objectAtIndex:indexPath.row];
    [[cell username] setText:[userName objectForKey:@"first name"]];

    NSDictionary *userlast = [self.neighbourData objectAtIndex:indexPath.row];
    [[cell lastName] setText:[userlast objectForKey:@"last name"]];

    NSDictionary *userBio = [self.neighbourData objectAtIndex:indexPath.row];
    [[cell userDescription] setText:[userBio objectForKey:@"userbio"]];



    NSString *profilePath = [[self.neighbourData objectAtIndex:indexPath.row] objectForKey:@"photo_path"];

    [cell.usermini sd_setImageWithURL:[NSURL URLWithString:profilePath]];

    NSLog(@"This is profilePath %@",profilePath);




    }
    return cell;

}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 115;
}

neighbourData日志:

[12663:3559832] This is the neighbourdata (
        {
        address = "1144 fake street";
        city = Las Vegas;
        "first name" = Panda;
        "last name" = Zoo;
        "photo_path" = "none";
      }

4 个答案:

答案 0 :(得分:1)

试试这个

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
    {
       NSPredicate * predicate =[NSPredicate predicateWithFormat:@"%K contains[cd] %@",@"first name", searchText];
       searchResults = [self.neighbourData filteredArrayUsingPredicate:resultPredicate];
     if searchResults.count == 0
     {
        NSPredicate * predicate =[NSPredicate predicateWithFormat:@"%K contains[cd] %@",@"last name",searchText];
        searchResults = [self.neighbourData filteredArrayUsingPredicate:resultPredicate];
     }
// add predicates for other keys also if you want

        [tableView reloadData];
    }

建议:

  • 避免使用单词之间的空格(不建议使用'名字',建议使用'firstName')

  • 以小写字母保存值('Panda','Zoo'最好保存为'panda','zoo'这样可以使搜索更简单)

答案 1 :(得分:0)

方法" filterContentForSearchText"应该在" textDidChange" UISearchBar的方法。你现在在哪里写这个方法?

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
@interface ViewController : UIViewController <UITextFieldDelegate, UITableViewDelegate, UITableViewDataSource, UISearchDisplayDelegate, UISearchBarDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) IBOutlet UISearchDisplayController *searchDisplayController;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.neighbourData = @[@"Apple", @"App", @"Bell"];


    NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];


    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"filterCell"];


}



    - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
    {
        NSPredicate *resultPredicate = [NSPredicate
                                        predicateWithFormat:@"SELF contains[cd] %@",
                                        searchText];

        self.searchResults = [self.neighbourData filteredArrayUsingPredicate:resultPredicate];
    }

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

     return YES;
     } */


    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
        // Return the number of sections.
        return 1;
    }

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


        if (tableView == self.searchDisplayController.searchResultsTableView) {
            return [self.searchResults count];

        } else {

            return [self.neighbourData count];

        }
    }

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

        UITableViewCell  * cell = [tableView dequeueReusableCellWithIdentifier:@"filterCell"];

        if (cell == nil) {

            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"filterCell"];
        }



        if (tableView == self.searchDisplayController.searchResultsTableView) {

            cell.textLabel. text = [self.searchResults objectAtIndex:indexPath.row];



        } else {

            cell.textLabel. text = [self.neighbourData objectAtIndex:indexPath.row];

        }

        return cell;

@end

答案 2 :(得分:0)

您需要在数据过滤后重新加载tableview,并删除searchResult数组的现有对象。

   - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
    {
      NSPredicate * predicate =[NSPredicate predicateWithFormat:@"first name = %@", searchText];
     searchResults = [self.neighbourData filteredArrayUsingPredicate:resultPredicate];
     [tableView reloadData];
    }

答案 3 :(得分:0)

试试这个谓词块:

NSPredicate* p = [NSPredicate predicateWithBlock:
                  ^BOOL(id obj, NSDictionary *d) {
                      NSString* s = obj;
                      NSStringCompareOptions options = NSCaseInsensitiveSearch;
                      return ([s rangeOfString:sbc.searchBar.text 
                                       options:options].location != NSNotFound);
                  }];
self.filteredStates = [states filteredArrayUsingPredicate:p];

同时结帐Link

希望它有所帮助。