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