1
我的數據結構,在字典中的所有臺詞是:搜索超過使用NSPredicate
現在我遍歷目錄,並添加「名稱」字符串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];
});
});
}
好的,那工作......但我怎麼在我的表中顯示我的過濾結果?在cell.textLabel.text中的名稱和cell.detailTextLabel.text中的地址?十分感謝! – 2013-04-29 15:24:37
爲什麼只在最後一個數組中搜索? – 2013-04-30 14:58:39
?代碼示例將搜索存儲在您的字典中的所有數組。你將不得不解釋你遇到的任何問題。 – Wain 2013-04-30 15:14:30