2013-01-11 86 views
1

我有一個包含數組中的多個字典的Plist。從Plist中搜索

我無法從Plist中搜索。當我從下面的代碼搜索時,我只能搜索輸入完整名稱objectForKey:@"aName",就像我需要輸入Sugar ..我無法使用sug..或s..I搜索將不得不打字糖,如果我搜索plist中的牛奶..它會顯示我的第一個字典,糖和plist顯示如下。 我必須搜索以 objectForKey: @ 「aName」

從plist.please檢查那裏我得到錯誤的搜索...

eneter image

- (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; 
     } 
+0

「我無法從Plist中搜索」 - 確切地說? – 2013-01-11 13:12:14

+0

對我來說你的plist看起來像和數組,但你把它放到字典中... –

+0

valueArray和mySection是數組 – Christien

回答

3

它爲您提供的過濾NSArray,你可以用結果在做其他事情。如果你正在使用searchBar你應該使用它的委託方法

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); 
+0

亞,但這上面的多個詞典plist不只是兩個...我不能去'糖'只有 – Christien

+0

然後設置關鍵字自由...它可以設置爲任何如你看到的。 – holex

+0

你沒有得到..我必須從密鑰@「aName」的plist搜索..和密鑰@「aName」將在數量爲100s ...我不必搜索任何特定的 – Christien

0

試試這個它可以幫助你......

,並從嘗試使用searchTableView方法..

爲如.. .try this

-(void)searchBar:(UISearchBar *)theSearchBar textDidChange:(NSString *)searchText { 


if([searchText length] > 0) { 
    searching = YES; 
    [self **searchTableView**:yourSearchBar]; ///give call to the **searchTableView** method.. 

    //show your search table.. 
} 

else { 
    searching = NO; 

    //set your search table hidden 
} 


} 



-(void)searchTableView:(UISearchBar *)searchBar { 

if (searchBar == yourSearchBar) { 
    NSString *searchText = yourSearchBar.text; 

    for (NSString *sTemp in yourValueArray) { 
     NSComparisonResult result = [sTemp compare:searchText options: (NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0, [searchText length])]; 

     if (result == NSOrderedSame) { 
      [self.yourTemporarySearchArray addObject:sTemp]; 


     } 
    } 

    [yourSearchTable reloadData]; 
} 

}