I am trying to implement the content serch in iPhone using UISearchBar my code is as below.
- (void)searchBar:(UISearchBar*)searchBar textDidChange:(NSString*)searchText {
if([tableData count])
[tableData removeAllObjects];
//dataArray contains array of strings
//tableData should containg arrow of strings as a serch result
NSRange range = {0, [searchText length]};
for(int i=0; i<[dataArray count]; ++i)
{
NSString *name = [dataArray objectAtIndex:i];
if([name compare:searchText options:NSCaseInsensitiveSearch range:range] == NSOrderedSame)
[tableData addObject:[dataArray objectAtIndex:i]];
}
[theTableView reloadData];
}
I am taking dataArray as a source of strings and then I want to search some content in each strings and then I need to store those strings which contains the search text, in the tableData array.
Write now I am getting only those strings as a search result in the starting text is matching as if in the dataArray there are 3 objects 1. India, 2. China, 3. America and I am serching “in” then it should show India and China both but it showing only India because “In” is starting content of India.
Please suggest me how to achieve the required result.
To seach anywhere in the string you need to check the whole string to see if the searchTest appears anywhere: