0

我有一个从 json 字符串解析的表格视图。一切都很好。如果我在我的搜索栏中输入一个字符,我得到这个错误'-[__NSCFString countByEnumeratingWithState:objects:count:]: unrecognized selector sent to instance 0x1d52ac50'下面是代码。如果下面的变量名数据是 NSMutableArray。你们能帮帮我吗。

- (BOOL)searchDisplayController:(UISearchDisplayController *)controllershouldReloadTableForSearchString:(NSString *)searchString
    {
[searchData removeAllObjects];

NSArray *group;
for(group in nameData)
{
    NSLog(@"wat am i doing here:%@",group);
    NSMutableArray *newGroup = [[NSMutableArray alloc] init];
    NSString *element;

    for(element in group)
    {
        NSRange range = [element rangeOfString:searchString options:NSCaseInsensitiveSearch];

        if (range.length > 0) {
            [newGroup addObject:element];
        }
    }

    if ([newGroup count] > 0) {
        [searchData addObject:newGroup];
    }

}

return YES;
 }
4

1 回答 1

1

请查看此代码

 - (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
    NSMutableArray *array=[[NSMutableArray alloc]init];
    NSDictionary *naming= [self.friendsDictionary objectForKey:@"data"];
    NSLog(@"ggg %@",[self.friendsDictionary objectForKey:@"data"]);
    for(int i =0 ; i< [[self.friendsDictionary objectForKey:@"data"] count]; i++){
        [array addObject:[[[self.friendsDictionary objectForKey:@"data"] objectAtIndex:i] objectForKey:@"name"]];
    }
    NSLog(@"Array %@", array);

    if(self.searchDisplayController.searchBar.text.length>0) 
    {
        NSString *strSearchText = self.searchDisplayController.searchBar.text;
        NSMutableArray *group = [[NSMutableArray alloc]init];
        for (int i=0; i<array.count; i++) {

            NSString *strData = [array objectAtIndex:i]; 
              NSLog(@"string Data:%@",strData);
            NSRange rng = [strData rangeOfString:strSearchText options:NSCaseInsensitiveSearch];
            if(rng.location != NSNotFound)
            {
                if(rng.location== 0)//that is we are checking only the start of the names.
                {
                    [group addObject:strData]; 
                }
            }
        }
        if ([group count] > 0) 
        {
    searchData = [[NSMutableArray alloc] init];
            [searchData addObjectsFromArray:group];
            NSLog(@"Sear %@", searchData);

        }
    } 
    return YES;
  }
于 2012-07-27T10:59:29.620 回答