0

我一直在玩弄我的應用程序表視圖的搜索工具一段時間,現在試圖讓它工作,但我一直在我的控制檯中得到相同的錯誤。TableView UISearchBar搜索時崩潰

終止應用程序由於未捕獲的異常「NSInvalidArgumentException」,原因:'[NSCFDictionary rangeOfString:選項:]:無法識別的選擇發送到實例

數據源是從字典取ITZ值的陣列.. .. 和tableData將存儲將顯示在表中的數據。 問: 假設我有一個包含5個值的字典,每個值都具有與這些值相對應的不同鍵。然後我將該字典放入數組中。該數組可以用作搜索的數據源嗎?我在cellForRowAtIndexPath中使用相同的數組來在我的單元格上顯示數據。

Plz建議使用代碼片段。

這裏是我的textDidChange

-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText 
    { 
     [tableData removeAllObjects];// remove all data that belongs to previous search 

     if([searchText isEqualToString:@""]){ 
      searchText==nil; 
      [tableview reloadData];  
      return;  
     } 

     NSInteger counter = 0; 
     for(NSString *name in dataSource)  
     {  
      NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];  
      NSRange r = [name rangeOfString:searchText options:NSCaseInsensitiveSearch];   
      if(r.location != NSNotFound)    
      {   
       if(r.location== 0)//that is we are checking only the start of the names.     
       {    
        [tableData addObject:name];    
       }   
      }  
      counter++;  
      [pool release];  
     } 
     [tableview reloadData]; 
    } 

回答

0

有附加條件try代碼...

if([searchText isEqualToString:@""] || searchText == nil){ 
    } 

讓我知道,它爲你工作或沒有?

如果數據源將是你的NSMutableArray ....

for (int i = 0; i < [dataSource count]; i++) 
    { 
     NSMutableDictionary *temp = (NSMutableDictionary*) [dataSource objectAtIndex:i]; 
     NSString *name = [NSString stringWithFormat:@"%@", [temp valueForKey:@"name"]]; 
     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];  
     NSRange r = [name rangeOfString:searchText options:NSCaseInsensitiveSearch];   
     if(r.location != NSNotFound)    
     {   
      if(r.location== 0)//that is we are checking only the start of the names.     
      {    
       [tableData addObject:name];    
      }   
     }  
     counter++;  
     [pool release]; 
    } 
+0

..no它不工作。:( –

+0

在這行完全是,正在顯示異常? –

+0

** NSRange R = [名rangeOfString:searchText options:NSCaseInsensitiveSearch]; ** –