1

嗨,在我的应用程序中,我需要搜索几个元素,结果必须显示在 tableview 中。例如,如果元素是

NS1,NSE2,Nase3,NWe3,Nxw,NB22 像这样,如果我搜索 N,那么数据必须显示如下

NAse3
NB22
NSE2
NS1
NWe3
Nxw. 

请让我知道如何实现这一点。

现在我正在使用下面的代码执行搜索。

-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    if([array count]!=0){
        array2=[[NSMutableArray alloc]initWithCapacity:0 ];
        for(NSMutableDictionary *data in array){
            r = [[data objectForKey:@"Product"] rangeOfString:searchBar.text options:NSCaseInsensitiveSearch];
            if(r.location != NSNotFound){
                 if(r.location== 0)
                     [array2 addObject:data];
            }
       }
    }
}
4

3 回答 3

2
-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    if([array count]!=0)
    {
        array2=[[NSMutableArray alloc]initWithCapacity:0];

        for(NSMutableDictionary *data in array)
        {
            r = [[data objectForKey:@"Product"] rangeOfString:searchBar.text options:NSCaseInsensitiveSearch];

            if(r.location != NSNotFound)
            {
                 if(r.location== 0)
                 [array2 addObject:data];
             }
       }
       NSSortDescriptor *sortDis = [[NSSortDescriptor alloc] initWithKey:@"Product"
                      ascending:YES selector:@selector(localizedStandardCompare:)];
      [array2 sortUsingDescriptors:[NSArray arrayWithObject:sortDis]];
    }
}
于 2013-11-07T09:52:52.727 回答
1

试试这个对你的结果进行排序:

array2 = [array2 sortedArrayUsingComparator:^(id a, id b) {
    return [a compare:b options:NSNumericSearch];
}];
于 2013-11-07T09:55:27.627 回答
0
NSSortDescriptor *sortDescriptor;
sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"your Key"
                                              ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
NSArray *sortedArray;
sortedArray = [drinkDetails sortedArrayUsingDescriptors:sortDescriptors];

可以使用自定义比较器方法查看文档

于 2013-11-07T09:56:24.440 回答