1

我的数据结构是:

在此处输入图像描述

现在我遍历列表并添加“名称”字符串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];


        });



    });


}
4

1 回答 1

8

如果您的字典只包含数组,那么您可以遍历字典中的键并单独过滤每个键(免费编写的代码):

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

如果需要,您还可以访问原始密钥。最后列出所有匹配项(匹配的字典数组)。

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

于 2013-04-29T15:04:33.607 回答