I am trying to implement Autocomplete method in an app. As I type type characters it returns words which matches first character.
For example if I type “B” or “ba” it returns string starting with ‘ba’ like “Ballon” and “Ball”. But in my array there are string with more than one word which are separated by either ‘(‘ or ‘space’. Example “White Ball” or ” “Giant (Big)”. I need to get those string as well. Any help will be highly appreciated.
Here is the code I am using,
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
autocompleteTableView.hidden = NO;
NSString *substring = [NSString stringWithString:textField.text];
substring = [substring stringByReplacingCharactersInRange:range withString:string];
[self searchAutocompleteEntriesWithSubstring:substring];
return YES;
}
- (void)searchAutocompleteEntriesWithSubstring:(NSString *)substring {
[autocompleteList removeAllObjects];
for(NSString *curString in contentArray)
{
NSRange substringRange = [curString rangeOfString:substring options:NSCaseInsensitiveSearch];
if (substringRange.location == 0)
{
[autocompleteList addObject:curString];
}
}
[autocompleteTableView reloadData];
}
Thanks
The problem lies within your if condition
Since you are checking for location 0, only strings starting with the substring will be returned. Changing the condition as follows should help you fix the problem.