2012-07-07 74 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]; 
     } 
    } 
} 

}