UISearchBar - 搜索对象数组的 NSDictionary

发布于 2024-10-11 04:43:53 字数 1088 浏览 7 评论 0原文

我正在尝试在表视图中插入一个搜索栏,该搜索栏加载了来自数组 NSDictionary 的信息。每个数组保存 和 对象。每个对象都有多个属性,例如名称或地址。

我已经实现了 NSSearchBar 的方法,但是与搜索本身相对应的代码(我在另一个项目中工作,其中数组只有字符串)不起作用,并且我无法解决这个问题。

这是代码: 'indiceLateral' 是一个带有字母表的数组; 'partners' 是一个 NSDictionary; “RLPartnersClass”是我的合作伙伴类别,每个合作伙伴都有属性(名称、地址……)。

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

有人可以帮我吗?

谢谢,

鲁伊·洛佩斯

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

愛放△進行李 2024-10-18 04:43:53

我已经做到了。

例子:

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

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];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文