IPhone SDK - TableSearch - Search all words instead of the first

I am playing with a sample TableSearch application from Apple.

In their application, they have an array with Apple products. There is one row with "iPod touch". When you search for “touch,” the results are not displayed.

Can someone help me make all the words in each search line? For the results to be found when searching for “iPod,” but also for the keyword “touch”.

Greetings.

+4
source share
1 answer

Below is the corresponding code in the filterContentForSearchText:scope: method in MainViewController.m:

 NSComparisonResult result = [product.name compare:searchText options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0, [searchText length])]; if (result == NSOrderedSame) { [self.filteredListContent addObject:product]; } 

This compares the first n characters (specified by the range parameter), ignoring the case and diacritics of each line with the first n characters of the current search line, where n is the length of the current search line.

Try changing the code to the following:

 NSRange result = [product.name rangeOfString:searchText options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch)]; if (result.location != NSNotFound) { [self.filteredListContent addObject:product]; } 

It searches for each row for the current search string.

+14
source

All Articles