我希望有人可以帮助我。我有一个搜索栏实现,它确实按照预期的方式过滤结果。问题是当我从过滤结果中选择时,它从原始数组中选择。根据我的阅读,一种方法是从过滤后的数组中删除所有项目并添加所选项目。另一种方法是删除所有与搜索条件不匹配的项目。
问题是我不知道该怎么做 - 尽管经过数小时的搜索。我会很感激任何正确方向的帮助或指示。
这是我的代码:
(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 :(得分: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];
}
}
}
}