2013-04-27 59 views
0

對我的表數據執行搜索過濾器現在我的搜索工作是sectionsTitle,如果我編寫「Category1」或「Category2」,它會找到Section1或Category2部分,但是我需要在所有這一切中通過NAMES搜索部分,從這裏:無法使用NSPredicate

NSDictionary *dict = [[tableData objectForKey:[sectionsTitle objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row]; 
[NSString stringWithFormat:@"%@", [dict objectForKey:@"Name"]]; 

我需要在我的代碼更改通過尋找?現在我很困惑與所有的NSArray的,NSMutableArray裏的和的NSDictionary :(

我加載像這樣我的數據:

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]; 

sectionKeys = [NSMutableArray new]; 
sectionsTitle = [NSMutableArray new]; 


     if ([[NSUserDefaults standardUserDefaults] boolForKey:@"blueKey"]) 
     { 

      ann = [dict objectForKey:@"Category1"]; 
      [resultArray addObject:@"Category1"]; 
      [resultDic setValue:ann forKey:@"Category1"]; 
      [sectionKeys addObject:@"Section 1"]; 

     } 


     if ([[NSUserDefaults standardUserDefaults] boolForKey:@"yellowKey"]) 
     { 
      ann = [dict objectForKey:@"Category2"]; 
      [resultArray addObject:@"Category2"]; 
      [resultDic setValue:ann forKey:@"Category2"]; 
      [sectionKeys addObject:@"Section 2"]; 

     } 


self.tableData = resultDic; 
self.sectionsTitle = resultArray; 

[myTable reloadData]; 

這是我如何過濾數據:

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope 
{ 
    NSPredicate *resultPredicate = [NSPredicate 
            predicateWithFormat:@"SELF contains[cd] %@", 
            searchText]; 
    searchResults = [sectionsTitle filteredArrayUsingPredicate:resultPredicate]; 

} 

-(BOOL)searchDisplayController:(UISearchDisplayController *)controller 
shouldReloadTableForSearchString:(NSString *)searchString 
{ 
    [self filterContentForSearchText:searchString 
           scope:[[self.searchDisplayController.searchBar scopeButtonTitles] 
             objectAtIndex:[self.searchDisplayController.searchBar 
                selectedScopeButtonIndex]]]; 

    return YES; 
} 

這是我的表格如何:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 

     if (tableView == self.searchDisplayController.searchResultsTableView) { 
      return 1; 
     }else{ 
     return sectionKeys.count; 
     } 

} 


- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { 

     if (tableView == self.searchDisplayController.searchResultsTableView) { 
      return @"Search"; 
     }else{ 
      return [sectionKeys objectAtIndex:section]; 
     } 

} 



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 

     if (tableView == self.searchDisplayController.searchResultsTableView) { 

      return [searchResults count]; 

     } else { 
      int num = [[tableData objectForKey:[sectionsTitle objectAtIndex:section]] count]; 
      return num; 
     } 

} 


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

     static NSString *CellIdentifier = @"Cell"; 

     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
     if (cell == nil) 
      cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; 


     if (tableView == self.searchDisplayController.searchResultsTableView) { 

      cell.textLabel.text = [searchResults objectAtIndex:indexPath.row]; 

     } else { 
      NSDictionary *dict = [[tableData objectForKey:[sectionsTitle objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row]; 


      cell.textLabel.font = [UIFont fontWithName:@"Avenir" size: 16.0]; 
      cell.detailTextLabel.font = [UIFont fontWithName:@"Avenir" size: 12.0]; 

      cell.textLabel.text = [NSString stringWithFormat:@"%@", [dict objectForKey:@"Name"]]; 
      cell.detailTextLabel.text = [NSString stringWithFormat:@"%@", [dict objectForKey:@"Address"]]; 

     } 

     return cell; 
} 

我的數據結構:

enter image description here

+0

我編輯了代碼並添加了一個數據結構圖像 – 2013-04-27 15:58:03

+0

每個類別都有很多字典Item0,Item1等 – 2013-04-27 16:23:00

回答

0

憑藉簡化的數據集(包含在字典中唯一的名稱項),這應該複製您的設置:

NSArray *categories = @[ 
    @[@{@"Name":@"Joe"}, @{@"Name":@"Jane"}], 
    @[@{@"Name":@"Anne"}, @{@"Name":@"Bob"}] 
]; 

然後ANY操作NSPredicate會發現你的數組後:

NSString *nameToSearch = @"Bob"; 
NSPredicate *catPred = [NSPredicate predicateWithFormat:@"ANY Name = %@", nameToSearch]; 

看到這個謂詞是如何篩選類別排列:

NSArray *filteredCats = [categories filteredArrayUsingPredicate:catPred]; 

順便說一句,如果您構建一些自定義對象來保存您的數據而不僅僅依賴於數組和字典,您可能會感到困惑。

+0

對不起,我不知道如何實現你的答案我的代碼 – 2013-04-27 16:46:14