我的数据结构是:
现在我遍历列表并添加“名称”字符串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];
});
});
}