开发者

UISearchBar - search a NSDictionary of Arrays of Objects

I'm trying to insert a search bar in a tableview, that is loaded with information from a NSDictionary of Arrays. Each Array holds and object. Each object has several properties, such as Name or Address.

I've implemented the methods of NSSearchBar, but the code corresponding 开发者_如何学运维to the search it self, that i have working on another project where the Arrays have strings only, is not working, and I can't get to thr problem.

Here's the code: 'indiceLateral' is a Array with the alphabet; 'partners' is a NSDictionary; 'RLPartnersClass' is my class of Partners, each one with the properties (name, address, ...).

-(void)handleSearchForTerm:(NSString *)searchTerm {

 NSMutableArray *sectionsToRemove = [[NSMutableArray alloc] init];
 [self resetSearch];

 for (NSString *key in self.indiceLateral) {
  NSMutableArray *array = [partners valueForKey:key];
  NSMutableArray *toRemove = [[NSMutableArray alloc] init];

  for (NSString *name in array) {
   if ([name rangeOfString:searchTerm options:NSCaseInsensitiveSearch].location == NSNotFound)
    [toRemove addObject:name];
  }

  if ([array count] == [toRemove count])
   [sectionsToRemove addObject:key];
  [array removeObjectsInArray:toRemove];
  [toRemove release];
 }

 [self.indiceLateral removeObjectsInArray:sectionsToRemove];

 [sectionsToRemove release];
 [theTable reloadData];
}

Can anyone help me please?

Thanks,

Rui Lopes


I've done it.

Example:

-(void)handleSearchForTerm:(NSString *)searchTerm {

    NSMutableDictionary *finalDict = [NSMutableDictionary new];
    NSString *currentLetter = [[NSString alloc] init];

    for (int i=0; i<[indiceLateral count]; i++) {
        NSMutableArray *elementsToDict = [[[NSMutableArray alloc] init] autorelease];
        currentLetter = [indiceLateral objectAtIndex:i];

        NSArray *partnersForKey = [[NSArray alloc] initWithArray:[partnersCopy objectForKey:[indiceLateral objectAtIndex:i]]];

        for (int j=0; j<[partnersForKey count]; j++) {
            RLNames *partnerInKey = [partnersForKey objectAtIndex:j];

            NSRange titleResultsRange = [partnerInKey.clientName rangeOfString:searchTerm options:NSDiacriticInsensitiveSearch | NSCaseInsensitiveSearch];

            if (titleResultsRange.length > 0){
                NSLog(@"found: %@", partnerInKey.clienteCity
                [elementsToDict addObject:partnerInKey];
            }
        }

        [finalDict setValue:elementsToDict forKey:currentLetter];
    }

    NSMutableDictionary *finalResultDict = [finalDict mutableDeepCopy];
    self.partners = finalResultDict;
    [finalResultDict release];

    [theTable reloadData];
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜