0

我希望有一个人可以帮助我。我实现了一个搜索栏,它确实按照预期的方式过滤了结果。问题是当我从过滤结果中选择时,它是从原始数组中选择的。根据我的阅读,一种方法是从过滤后的数组中删除所有项目并添加所选项目。另一种方法是删除所有与搜索条件不匹配的项目。

问题是我不知道该怎么做——尽管搜索了几个小时。对于正确方向的任何帮助或指示,我将不胜感激。

这是我的代码:

(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];

}
4

1 回答 1

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];
        }
    }
}

}

于 2012-07-23T21:05:22.287 回答