2013-04-29 101 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