Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone SDK - TableSearch - Search all words instead of only the first one

Tags:

iphone

I'm playing with the TableSearch sample application from Apple.

In their application, they have an array with Apple products. There is one row with "iPod touch". When searching for "touch", no results are displayed.

Can someone help me making all the words in each row searchable? So that results are found when searching for "iPod" but also for the keyword "touch".

Cheers.

like image 302
nicoko Avatar asked Dec 10 '22 20:12

nicoko


1 Answers

Below is the relevant 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 case and diacritics, of each string with the first n characters of the current search string, where n is the length of the current search string.

Try changing the code to the following:

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

This searches each string for the current search string.

like image 177
titaniumdecoy Avatar answered May 20 '23 14:05

titaniumdecoy