我有一个在数组中具有多个词典的Plist。
我无法从Plist
中进行搜索。当我从以下代码中进行搜索时,仅当输入objectForKey:@"aName"
的全名时才可以进行搜索,例如我需要输入Sugar ..我无法使用sug ..或s ..进行搜索,我必须输入糖满了,如果我搜索plist中的Milk ..它将显示糖和plist的第一个字典,如下所示。
我必须搜索才能
objectForKey:@“aName”
从plist.please检查我错在哪里搜索...
- (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;
}
最佳答案
它为您提供了过滤后的NSArray
,您可以进一步处理结果。
NSArray *_originalArray = ...;
/*
* EDITED : because the thread's owner has seriously no idea what it is.
*
* you can get the searchText's value from anywhere else of your code
* (i.e. from UITextField, UISearchBar, and anywhere else...)
*
*/
NSString *searchText = ...; // EDITED : this is a parameter only, it can be set freely
NSArray *_filteredArray = [originalArray filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) {
NSDictionary *_dataRow = (NSDictionary *)evaluatedObject;
return ([[[_dataRow valueForKey:@"aName"] lowercaseString] rangeOfString:[searchText lowercaseString]].location != NSNotFound);
}]];
NSLog (@"%@", _filteredArray);
关于iphone - 从列表中搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14278787/