2013-04-29 64 views
1

我的数据结构,在字典中的所有台词是:搜索超过使用NSPredicate

enter image description here

现在我遍历目录,并添加“名称”字符串NSMutableArray然后搜索并使用NSPredicate过滤器的数据。

如何搜索整个列表?通过名称 & 地址所有数组中的字符串?

过滤数据:

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

    searchResults = [sectionsNames filteredArrayUsingPredicate:resultPredicate]; 
} 

加载数据:

#pragma mark - 
#pragma mark Load plist file 
- (void)loadPList 
{ 

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 

     NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); 
     NSString *path = [[documentPaths lastObject] stringByAppendingPathComponent:@"data.plist"]; 
     NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path]; 

     NSMutableDictionary *resultDic = [[NSMutableDictionary alloc] init]; 
     NSMutableArray *resultArray = [[NSMutableArray alloc] init]; 
     NSMutableArray *resultName = [[NSMutableArray alloc] init]; 

     sectionKeys = [NSMutableArray new]; 
     sectionsTitle = [NSMutableArray new]; 
     sectionsNames = [NSMutableArray new]; 

     dispatch_async(dispatch_get_main_queue(), ^{ 


      NSMutableArray *annotations = [[NSMutableArray alloc]init]; 

      NSMutableArray *annotationsToRemove = [ mapView.annotations mutableCopy ] ; 
      [ annotationsToRemove removeObject:mapView.userLocation ] ; 
      [ mapView removeAnnotations:annotationsToRemove ] ; 

    ann = [dict objectForKey:@"Blue"]; 
       [resultArray addObject:@"Blue"]; 
       [resultDic setValue:ann forKey:@"Blue"]; 
       [sectionKeys addObject:@"Siwa"]; 


       for(int i = 0; i < [ann count]; i++) { 


        NSString *coordinates = [[ann objectAtIndex:i] objectForKey:@"Coordinates"]; 

        double realLatitude = [[[coordinates componentsSeparatedByString:@","] objectAtIndex:1] doubleValue]; 
        double realLongitude = [[[coordinates componentsSeparatedByString:@","] objectAtIndex:0] doubleValue]; 

        MyAnnotation *myAnnotation = [[MyAnnotation alloc] init]; 
        CLLocationCoordinate2D theCoordinate; 
        theCoordinate.latitude = realLatitude; 
        theCoordinate.longitude = realLongitude; 

        myAnnotation.coordinate=CLLocationCoordinate2DMake(realLatitude,realLongitude); 
        myAnnotation.title = [[ann objectAtIndex:i] objectForKey:@"Name"]; 
        myAnnotation.subtitle = [[ann objectAtIndex:i] objectForKey:@"Address"]; 
        myAnnotation.icon = [[ann objectAtIndex:0] objectForKey:@"Icon"]; 

        NSString *name = [[ann objectAtIndex:i] objectForKey:@"Name"]; 
        [resultName addObject:name]; 

        [mapView addAnnotation:myAnnotation]; 
        [annotations addObject:myAnnotation]; 


       } 

    self.tableData = resultDic; 
      self.sectionsTitle = resultArray; 
      self.sectionsNames = resultName; 

      [myTable reloadData]; 


     }); 



    }); 


} 

回答

7

如果你的字典里只包含数组,那么你可以循环遍历字典的键和单独过滤每个(自由编写的代码):

NSPredicate *resultPredicate = [NSPredicate 
           predicateWithFormat:@"Name CONTAINS[cd] %@ OR Address CONTAINS[cd] %@", 
           searchText, 
           searchText]; 

NSMutableArray *allMatches = [NSMutableArray array]; 

for (NSString *key in myDictionary) { 
    NSArray *array = [myDictionary objectForKey:key]; 

    NSArray *matches = [array filteredArrayUsingPredicate:resultPredicate]; 

    if (matches.count > 0) { 
     [allMatches addObjectsFromArray:matches]; 
    } 
} 

这样做,您还可以访问原始密钥,如果您需要d it。并且你完成了所有匹配的列表(一组匹配的字典)。

现在在您的表格视图中,您可以返回所有匹配计数的行数,并从字典中获取NameAddress以显示。

+0

好的,那工作......但我怎么在我的表中显示我的过滤结果?在cell.textLabel.text中的名称和cell.detailTextLabel.text中的地址?十分感谢! – 2013-04-29 15:24:37

+0

为什么只在最后一个数组中搜索? – 2013-04-30 14:58:39

+0

?代码示例将搜索存储在您的字典中的所有数组。你将不得不解释你遇到的任何问题。 – Wain 2013-04-30 15:14:30