StuartM
StuartM

Reputation: 6813

Search Bar - Filtering an Array of search terms for a Collection View Data Source

I have a main plist file, which includes a collection views data source. The file has a Root array element with dictionaries for each category layed out in the following format:
Example Layout

The example shows one 'category'. These are loaded into an array for a UICollectionView and a property for access

self.mainDataSource = [[NSArray alloc] initWithContentsOfFile:plistPath];  

When I setup the cells I create a Dictionary instance for the indexPath.row and access the relevant objects. All this works fine.

Issue
What I am trying to do now is add a search bar. I have that all setup but I would like to filter the categories based on the word entered compared to the 'sub' array of search terms each category could have.

I have the following method which runs when they search, but I cannot figure out the best way to search the 'sub' array of the category Dictionaries.

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope{
    NSPredicate *resultPredicate    = [NSPredicate predicateWithFormat:@"self contains[c] %@", searchText];
    self.dataSourceForSearchResult  = [self.mainDataSource filteredArrayUsingPredicate:resultPredicate];
}

Upvotes: 0

Views: 396

Answers (1)

StuartM
StuartM

Reputation: 6813

NSPredicate *resultPredicate    = [NSPredicate predicateWithFormat:@"(ANY SearchTerms LIKE[cd] %@ OR ANY SearchTerms CONTAINS[cd] %@)", searchText, searchText];
self.dataSourceForSearchResult  = [self.mainDataSource filteredArrayUsingPredicate:resultPredicate];  

The trick was to include 'ANY' specifically for the LIKE to work. Then the name of the relevant sub array as key.

Upvotes: 1

Related Questions