配列に複数の辞書を持つ Plist があります。
から検索することができませんPlist
。以下のコードから検索する場合、objectForKey:@"aName"
Sugar を入力する必要があるなどのフルネームを入力した場合にのみ検索できます..sug..または s..で検索できません。そして、plistにあるMilkを検索すると..Sugarの最初の辞書が表示され、plistを以下に示します
..objectForKey:@"aName"を検索する必要があります
plist から検索するのにどこが間違っているかを確認してください...
- (void)viewDidLoad
{
[super viewDidLoad];
if (!expandedSections)
{
expandedSections = [[NSMutableIndexSet alloc] init];
}
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [documentPaths objectAtIndex:0];
NSString *documentPlistPath = [documentsDirectory stringByAppendingPathComponent:@"p.plist"];
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:documentPlistPath];
valueArray = [dict objectForKey:@"title"];
self.mySections=[valueArray copy];
NSLog(@"value array %@",self.mySections);
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
if (tableView==self.searchDisplayController.searchResultsTableView)
{
return [self.searchResults count];
}
else
{
return [self.mySections count];
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if ( !(tableView == self.searchDisplayController.searchResultsTableView) )
{
if ([expandedSections containsIndex:section] )
{
return [[[self.mySections objectAtIndex:section ] allKeys] count] ;
}
return 1;
} else
if(tableView == self.searchDisplayController.searchResultsTableView)
{
if ([expandedSections containsIndex:section] )
{
NSString *key = [self.searchResults objectAtIndex:section];
NSArray *dataInSection = [[self.mySections objectAtIndex:section ] allKeys] ;
return [dataInSection count];
}
}
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
}
NSUInteger section = [indexPath section];
NSUInteger row = [indexPath row];
NSString *key = nil;
if ([tableView isEqual:self.searchDisplayController.searchResultsTableView])
{
key = [self.searchResults objectAtIndex:section];
}
else{
key = [self.mySections objectAtIndex:section];
}
NSDictionary *dict = [self.mySections objectAtIndex:indexPath.section];
cell.textLabel.text = [dict.allKeys objectAtIndex:indexPath.row];
cell.detailTextLabel.text= [dict valueForKey:[dict.allKeys objectAtIndex:indexPath.row]];
return cell;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *tempView=[[UIView alloc]initWithFrame:CGRectMake(0,0,tableView.bounds.size.width,40)];
tempView.backgroundColor=[UIColor blackColor];
UILabel *tempLabel=[[UILabel alloc]initWithFrame:CGRectMake(0,3,tableView.bounds.size.width-10,40)];
tempLabel.backgroundColor=[UIColor clearColor];
tempLabel.textColor = [UIColor redColor]; //here you can change the text color of header.
tempLabel.font = [UIFont fontWithName:@"ChalkboardSE-Bold" size:13];
tempLabel.font = [UIFont boldSystemFontOfSize:13];
NSString *key=nil;
if ([tableView isEqual:self.searchDisplayController.searchResultsTableView])
{
key = [[self.mySections objectAtIndex:section]objectForKey:@"aName"];
}
else{
key = [[self.mySections objectAtIndex:section]objectForKey:@"aName"];
// return [NSString stringWithFormat:@"%@", key];
}
tempLabel.text=[NSString stringWithFormat:@"%@", key];
[tempView addSubview:tempLabel];
return tempView;
}
- (void)filterContentForSearchText:(NSString*)searchText
scope:(NSString*)scope
{
NSPredicate *resultPredicate = [NSPredicate
predicateWithFormat:@"SELF contains[cd] %@",
searchText];
self.searchResults = [self.mySections filteredArrayUsingPredicate:resultPredicate];
}
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
UISearchBar * searchBar = [controller searchBar];
[self filterContentForSearchText:searchString scope:[[searchBar scopeButtonTitles] objectAtIndex:[searchBar selectedScopeButtonIndex]]];
return YES;
}