I'm new with UISearchBar and i've got some code for searching words.
- (void)searchTableView
{
NSLog(@"Searching...");
NSString *searchText = searchBar.text;
NSMutableArray *tempSearch = [[NSMutableArray alloc] init];
for (NSDictionary *item in items) {
if ([[item objectForKey:@"en"] isEqualToString:searchText]) {
NSLog(@"Found");
[tempSearch addObject:item];
}
}
searchArray = [tempSearch copy];
}
It works but problem is it works only for complete word (e.g. If in textLabel
in UITableViewCell
is text "something" and i type "something" in UISearchBar
then i've got this row returned, but when i type "some" i've got rows when complete word is "some", "something" isn't showed). How can i search text correclty with my method?
Using NSPredicate
you can simplify your search a bit:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"en BEGINSWITH %@", searchText];
NSArray *tempSearch = [items filteredArrayUsingPredicate:predicate];
If you want case-insensitive search, replace BEGINSWITH
by BEGINSWITH[c]
.
See Using Predicates in the "Predicate Programming Guide" for more information.