The following code lets me auto suggest values typed into a UITextfield, by comparing it to an array of previously added string objects and show it in a UITableview. This works fine, but only for a single word.
So now, can I modify the code in such a way, that after the user enters a comma, then starts typing again, I can search the same string array for suggestions again for the characters typed after the comma?
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if (textField.tag == tagTextFieldTag) //The text field where user types
{
textField.autocorrectionType = UITextAutocorrectionTypeNo;
autocompleteTableView.hidden = NO; //The table which displays the autosuggest
NSString *substring = [NSString stringWithString:textField.text];
substring = [substring stringByReplacingCharactersInRange:range withString:string];
if ([substring isEqualToString:@""])
{
autocompleteTableView.hidden = YES; //hide the autosuggest table if textfield is empty
}
[self searchAutocompleteEntriesWithSubstring:substring]; //The method that compares the typed values with the pre-loaded string array
}
return YES;
}
- (void)searchAutocompleteEntriesWithSubstring:(NSString *)substring {
[autoCompleteTags removeAllObjects];
for(Tag *tag3 in tagListArray) //tagListArray contains the array of strings
//tag3 is an object of Tag class, which has a single attribute called 'text'
{
NSString *currentString = tag3.text;
NSRange substringRange = [currentString rangeOfString:substring];
if(substringRange.location ==0)
{
[autoCompleteTags addObject:currentString];
}
}
[autocompleteTableView reloadData];
}