开发者

TableView UISearchBar Crashes while Searching

I've been playing around with a search facility for my application table view for a while now trying to get it working but i keep getting the same error in my console.

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: ' [NSCFDictionary rangeOfString:options:]: unrecognized selector sent to instance

DataSource is an array which is taking itz value from a dictionary.... and tableData will be storing data that will be displayed in table. Q: Suppose I have a dictionary with 5 values each having different keys corresponding to those values.. then i put that dictionary in an array. Can that array be used as a dataSource for search? and i开发者_开发问答 am using the same array in cellForRowAtIndexPath to display data on my cells.

Plz suggest with code snippet.

Here is my code for 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]; 
    }


Try with additional condition...

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

Let me know, does it work for you or not?

If dataSource would be your 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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜