2012-07-07 76 views
0

我希望有人能帮助我。我有一个搜索栏实现,它会按照它应有的方式过滤结果。问题是,当我从筛选结果中选择时,它会从原始数组中进行选择。从我读过的,一种方法是从过滤数组中删除所有项目并添加所选项目。另一种方法是删除所有不符合搜索条件的项目。如何让UISearchBar从筛选结果中选择?

问题是,我不知道如何做到这一点 - 尽管几个小时的搜索。我会感谢任何正确方向的帮助或指引。

这里是我的代码:

(void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    self.tableView.scrollEnabled = YES; 

    categories = [NSArray arrayWithObjects: 
        @"Other", 
        @"Breakfast", 
        @"Chemist", 
        @"Computer", 
        @"Dinner", 
        @"Drinks", 
        @"Entertainment", 
        @"Fuel", 
        @"Groceries", 
        @"Haircut", 
        @"Hotel", 
        @"Internet", 
        @"Laundry", 
        @"Lunch", 
        @"Meals", 
        @"Medical", 
        @"Parking", 
        @"Snacks", 
        @"Stationery", 
        @"Taxis", 
        @"Telephone", 
        @"Transport", 
        @"Travel Taxes", 
        nil]; 

    self.allCatgeories = categories; 

    [self.tableView reloadData]; 

} 

(void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope 
{ 

    NSPredicate *resultPredicate = [NSPredicate 
            predicateWithFormat:@"SELF contains[cd] %@", 
            searchText]; 

    self.searchResults = [self.allCatgeories filteredArrayUsingPredicate:resultPredicate]; 

} 

回答

0

您可以在下面使用,它工作正常,我:

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope 
{ 
[self.filteredInboxItems removeAllObjects]; 
/* 
Search the main list for products whose type matches the scope (if selected) and whose name matches searchText; add items that match to the filtered array. 
*/ 
for (NSNumber *id in self.inboxItemDAO.inboxItemIDs) 
{ 
    NSDictionary *inboxItem = [self.inboxItemDAO getInboxItem:id]; 
    if ([scope isEqualToString:@"bla bla 1"]) 
    { 
     NSComparisonResult result = [[inboxItem objectForKey:@"Subject"] compare:searchText options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0, [searchText length])]; 
     if (result == NSOrderedSame) 
     { 
      [self.filteredInboxItems addObject:inboxItem]; 
     } 
    } 
    else if ([scope isEqualToString:@"bla bla2"]) 
    { 
     NSComparisonResult result = [[inboxItem objectForKey:@"Sender"] compare:searchText options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0, [searchText length])]; 
     if (result == NSOrderedSame) 
     { 
      [self.filteredInboxItems addObject:inboxItem]; 
     } 
    } 
    else if ([scope isEqualToString:@"bla bla 3"]) 
    { 
     NSComparisonResult result = [[inboxItem objectForKey:@"Action"] compare:searchText options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0, [searchText length])]; 
     if (result == NSOrderedSame) 
     { 
      [self.filteredInboxItems addObject:inboxItem]; 
     } 
    } 
} 

}