2016-12-13 21 views
2

Wenn mein Benutzer ein Element in meiner UISearchBar durchsucht, werden die Ergebnisse angezeigt, wenn das in die Leiste eingegebene vollständige Wort mit einem der Ergebnisse übereinstimmt. Zum Beispiel Wenn "Panda" eingegeben wird, erscheint Panda in der Tabelle Ergebnisse anzeigen. Wenn jedoch "Pan" eingegeben wird, werden keine Ergebnisse angezeigt. Wie kann ich meinen Suchergebnisfilter so arbeiten lassen, wie der Benutzer tippt? Panda, auch wenn nur "pan" werden sollte, wird angezeigt eingegebenUISearchBar zeigt keine Ergebnisse bei der Texteingabe

Mein Filter Code sieht derzeit wie folgt aus:.

.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 Log:

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

Antwort

1

versuchen, diese

- (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]; 
    } 

Vorschläge:

  • Vermeiden Sie Leerzeichen zwischen den Wörtern von Tasten ('Vorname' ist dies nicht zu empfehlen, 'vorName' empfohlen)

  • Und speichern Sie Werte in Kleinbuchstaben ('Panda', 'Zoo' ist es besser zu speichern als 'Panda', 'Zoo' das wird ma ke Suche einfachere)

+0

Das verursacht den folgenden SIGABRT-Fehler in dem Moment, in dem ich in der Suchleiste tippe: 'Die Format-Zeichenfolge kann nicht analysiert werden' Vorname enthält [cd]% @ "' – Brittany

+0

Der Raum ist definitiv der Grund für den Absturz - gibt es einen Weg zu suche dieses Feld, ohne den Platz zu entfernen? Zu diesem Zeitpunkt ist es zu spät für mich, den Feldnamen zu ändern:/lol help! – Brittany

+0

einmal mit dem aktualisierten Code versuchen – raki

0

Die Methode "filterContentForSearchText" sollte in "TextDidChange" -Methode von UISearchBar sein. Wo schreiben Sie gerade diese Methode?

- (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 
+0

Siehe Code bearbeiten. Wenn dies der Fall ist, in was soll mein BOOL geändert werden? Wenn Sie die Zeile ändern, die Sie vorgeschlagen haben, verursacht der BOOL einen Fehler. – Brittany

+0

Okay. Sie verwenden UISearchDisplayController (es ist in ios8 veraltet. Alternative ist UISearchController). Versuchen Sie, diesen Code zu kommentieren. Ich würde empfehlen, UISearchController zu verwenden, wenn Ihre App Leela

+0

Ich habe das BOOL auskommentiert. Selbst wenn die obige Codezeile vorhanden ist, filtert sie immer noch nicht, wenn ich in die Suchleiste eintippe:/Siehe Änderungen - fügt meinen vollständigen Suchcode ein. – Brittany

0

Sie benötigen eine Tableview, nachdem Ihre Daten gefiltert neu zu laden und auch gibt es für search Array-Objekt zu entfernen.

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

[searchResults removeAllObjects]; Das Hinzufügen dieser Zeile führt zu dem Fehler "Keine sichtbare Schnittstelle für NSArray deklariert den Selektor removeAllObjects". – Brittany

+0

ok dann entfernen Sie diese Zeile, da Ihr Array-Typ NSArray anstelle von NSMutableArray ist. – KKRocks

+0

Diese Zeile wurde entfernt - die Suchleiste filtert immer noch nicht, während ich Text eintippe. – Brittany

0

dieses Prädikat Block ausprobieren:

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]; 

Inzwischen diese Link Kasse.

Ich hoffe, es hilft.

+0

Was ist "sbc" ?? Undeklariert? – Brittany

+0

Verwenden Sie searchText anstelle von sbc.searchBar.text –

Verwandte Themen