tags:

views:

1049

answers:

2

Hello all,

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.

+7  A: 

Below is the relevant code in -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.

titaniumdecoy
Thank you so much. This works great!It now searches within strings, so with the above example it finds "iPod touch" even with "ouch" as keyword. But this is not a big problem for me.
nicoko
In that case, please mark my answer as accepted. Thanks.
titaniumdecoy
Thanks for the snippet. Works well!
jfroom
A: 

VERY HELPFUL!!! Thank you!

Kirn