2015-04-30 4 views
-1

Я сделал список в tableviewcontroller, затем добавил строку поиска и поиска. Но из-за того, что у меня есть массив в поиске на русском языке, не работает. Если я добавлю числа в массив и начну поиск, это будет работать. Но если по русским буквам нет. Как это исправить?массив поиска на русском языке

Мой код

- (instancetype)initWithCoder:(NSCoder *)coder 
{ 
    self = [super initWithCoder:coder]; 
    if (self) { 
     _items 
     = [NSArray arrayWithObjects: 
      @"А", 
      @"Ак", 
      @"Ба", 
      @"Бо", 
      @"22", 
      @"23", 
      nil]; 
    } 
    return self; 
} 

#pragma mark - UISearchDisplayDelegate 

// register a cell reuse identifier for the search results table view 
-(void)searchDisplayController:(UISearchDisplayController *)controller 
didLoadSearchResultsTableView:(UITableView *)tableView { 
    [tableView registerClass:[UITableViewCell class] 
     forCellReuseIdentifier:@"SearchResultsTableViewUITableViewCell"]; 
} 

// perform the search 
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller 
shouldReloadTableForSearchString:(NSString *)searchString { 
    NSPredicate *predicate 
    = [NSPredicate predicateWithFormat:@"self beginswith %@", searchString]; 
    NSArray *searchResults 
    = [[self items] filteredArrayUsingPredicate:predicate]; 
    [self setSearchResults:searchResults]; 

    return YES; 
} 

#pragma mark - UITableViewDataSource 

// check if displaying search results 
-(NSInteger)tableView:(UITableView *)tableView 
numberOfRowsInSection:(NSInteger)section { 
    if ([[self searchDisplayController] isActive]) { 
     return [[self searchResults] count]; 
    } else { 
     return [[self items] count]; 
    } 
} 

// check if displaying search results 
-(UITableViewCell *)tableView:(UITableView *)tableView 
     cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    if ([[self searchDisplayController] isActive]) { 
     UITableViewCell *cell 
     = [tableView dequeueReusableCellWithIdentifier:@"SearchResultsTableViewUITableViewCell" 
              forIndexPath:indexPath]; 
     id item = [[self searchResults] objectAtIndex:[indexPath row]]; 
     [[cell textLabel] setText:item]; 
     return cell; 
    } else { 
     UITableViewCell *cell 
     = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell" 
              forIndexPath:indexPath]; 
     id item = [[self items] objectAtIndex:[indexPath row]]; 
     [[cell textLabel] setText:item]; 
     return cell; 
    } 
} 

#pragma mark - UITableViewDelegate 

// manually perform detail segue after selecting a search result 
-(void)tableView:(UITableView *)tableView 
didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    if ([[self searchDisplayController] isActive]) { 
     UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
     [self performSegueWithIdentifier:@"detailSegue" sender:cell]; 
    } 
} 

#pragma mark - UIViewController 

/* prepare for detail scene segue 
called after cell selection in the master and 
search results table views */ 
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 
    UITableViewCell *cell = (UITableViewCell *)sender; 

    id item = nil; 
    if ([[self searchDisplayController] isActive]) { 
     NSIndexPath *indexPath 
     = [[[self searchDisplayController] searchResultsTableView] indexPathForCell:cell]; 
     item = [[self searchResults] objectAtIndex:[indexPath row]]; 
    } else { 
     NSIndexPath *indexPath 
     = [[self tableView] indexPathForCell:cell]; 
     item = [[self items] objectAtIndex:[indexPath row]]; 
    } 

    UIViewController *detail 
    = (UIViewController *)[segue destinationViewController]; 
    [[detail navigationItem] setTitle:item]; 
} 
@end 
+0

С '[NSPredicate predicateWithFormat: @ "SELF BEGINSWITH% @", @ "Б"]', кажется, работает. – Larme

+0

@ Larme это работа, но когда вы нажимаете любую кнопку, отображается список, начинающийся с "Б" – tarasikgoga

ответ

0
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF contains [cd] %@", self.searchBar.text]; 
     self.searchResults = [[_actualItems filteredArrayUsingPredicate:predicate] mutableCopy]; 
Смежные вопросы