2013-03-12 13 views
1

我已經將幾個教程拼湊在一起以創建一個帶分區的分組表,並且現在我試圖讓uisearchbar工作。我遇到的問題是如何在分組部分中進行搜索。分解段中的uisearchbar可用

我讀過這篇文章提出的類似的問題,但不能

這是創建分組部分

#import "Job.h"  // A model for the data 
#import "Address.h" // Another model for the data 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    self.theTable.delegate = self; 
    self.theTable.dataSource =self; 

    _searchBar.delegate = (id)self; 

    FMDBDataAccess *db = [[FMDBDataAccess alloc] init]; 

    jobs = [[NSMutableArray alloc] init]; 
    jobs = [db getJobs:1]; 

    _sections = [[NSMutableDictionary alloc] init]; 

    NSMutableArray *jobsTempArray = [db getJobsAsDictionary:1]; 

    BOOL found; 

    // Loop through the books and create our keys 
    for (NSDictionary *book in jobsTempArray) 
    { 
     NSString *cLong = [book objectForKey:@"addrAddress"]; 

     NSString *c = [cLong substringToIndex:1]; 

     found = NO; 

     for (NSString *str in [_sections allKeys]) 
     { 
      if ([str isEqualToString:c]) 
      { 
       found = YES; 
      } 
     } 

     if (!found) 
     { 
      [_sections setValue:[[NSMutableArray alloc] init] forKey:c]; 
     } 

    } 

    // Loop again and sort the books into their respective keys 
    for (NSDictionary *book in jobsTempArray) 
    { 
     [[_sections objectForKey:[[book objectForKey:@"addrAddress"] substringToIndex:1]] addObject:book]; 
    } 

    // Sort each section array 
    for (NSString *key in [_sections allKeys]) 
    { 
     [[_sections objectForKey:key] sortUsingDescriptors:[NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"addrAddress" ascending:YES]]]; 
    } 

} 

的代碼,這是搜索

-(void)searchBar:(UISearchBar*)searchBar textDidChange:(NSString*)text 
{ 
    if(text.length == 0) 
    { 
     _isFiltered = FALSE; 
    } 
    else 
    { 
     _isFiltered = true; 
     _filteredjobs = [[NSMutableArray alloc] init]; 

     //for (Job* book in jobs) 
     //for (Job* book in [_sections allKeys]) 
     //for (NSString *food in [_sections allKeys]) 
     for (NSDictionary* book in [_sections allKeys]) 
     { 
      NSString *addrStr = [book objectForKey:@"addrAddress"]; 
      NSString *postStr = [book objectForKey:@"addrPostcode"]; 

      //NSRange nameRange = [book.jobAddress rangeOfString:text options:NSCaseInsensitiveSearch]; 
      NSRange nameRange = [addrStr rangeOfString:text options:NSCaseInsensitiveSearch]; 

      //NSRange descriptionRange = [book.jobPostcode rangeOfString:text options:NSCaseInsensitiveSearch]; 
      NSRange descriptionRange = [postStr rangeOfString:text options:NSCaseInsensitiveSearch]; 

      if(nameRange.location != NSNotFound || descriptionRange.location != NSNotFound) 
      { 
       [_filteredjobs addObject:book]; 
      } 
     } 
    } 

    [self.theTable reloadData]; 
} 
代碼

我有儘可能多的意識到我需要改變for (Job* food in jobs)for (NSDictionary* book in [_sections allKeys]),但我堅持如何搜索[_sections allKeys]

具體來說這一行

NSRange nameRange = [addrStr rangeOfString:text options:NSCaseInsensitiveSearch]; 

崩潰 - [__ NSCFString objectForKey:]:無法識別的選擇發送到實例0x692e200

***終止應用程序由於未捕獲的異常' NSInvalidArgumentException',原因:' - [__ NSCFString objectForKey:]: 無法識別的選擇器發送到實例0x692e200':

有什麼想法? PS對待我作爲一個noob,我需要一些代碼以及解釋 - 我還在學習obj-c

回答

0

我在 UISearchBar - search a NSDictionary of Arrays of Objects找到答案,並閱讀allkeys。

基本上通過分組的NSDictionary環和提取NSArrays,然後通過再次搜索環......

-(void)searchBar:(UISearchBar*)searchBar textDidChange:(NSString*)text 
{ 
    if(text.length == 0) 
    { 
     _isFiltered = FALSE; 
    } 
    else 
    { 
     _isFiltered = true; 
     _filteredjobs = [[NSMutableArray alloc] init]; 

     NSString *currentLetter = [[NSString alloc] init]; 

     for (int i=0; i<[_sections count]; i++) 
     { 
      currentLetter = [[_sections allKeys] objectAtIndex:i]; 

      NSArray *jobsForKey = [ [NSArray alloc] initWithArray:[_sections objectForKey:[[_sections allKeys] objectAtIndex:i]] ]; 

      for (int j=0; j<[jobsForKey count]; j++) 
      { 
       NSDictionary *book = [jobsForKey objectAtIndex:j]; 
       NSRange titleResultsRange = [[book objectForKey:@"addrAddress"] rangeOfString:text options:NSCaseInsensitiveSearch]; 

       if(titleResultsRange.location != NSNotFound) 
       { 
        [_filteredjobs addObject:book]; 
       } 
      } 
     } 

    } 

    [self.theTable reloadData]; 
} 
0

檢查Link.它顯示UISearchBar與分組SectionViewview.Its一個簡單的教程..希望它的對你有用

+0

感謝您的鏈接,但它構建了搜索之後的分組部分,但搜索已分組的NSDictionary(實際上是基於我已經使用過的教程) – JulianB 2013-03-13 21:23:21