我想为UITextView做自定义自动完成...
例如:
If the user starts to type "Busines" I would like to suggest "Business1", "Business2", , so that the user can select any one of the 2 suggestions or choose to type a new one.
所有自定义字词建议都在数组中......
我怎样才能实现这个目标?
completionsForPartialWordRange:inString:language:
我可以使用的东西..如何传递数组中的值?
答案 0 :(得分:0)
您可以尝试使用此代码。
- (void)searchAutocompleteEntriesWithSubstring:(NSString *)substring
{
[autocomplete_array removeAllObjects];
for(int i=0;i<[your_main_array count];i++)
{
NSString *curString = [your_main_array objectAtIndex:i];
curString = [curString lowercaseString];
substring = [substring lowercaseString];
if ([curString rangeOfString:substring].location == NSNotFound)
{}
else
{
[autocomplete_array addObject:curString]
}
}
[autocompletedisplayTableView reloadData];
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
if( textView == your_TextView)
{
[your_TextView resignFirstResponder];
autocompletedisplayTableView.hidden = NO;
NSString *substring = [NSString stringWithString:your_TextView.text];
substring = [substring stringByReplacingCharactersInRange:range withString:string];
[self searchAutocompleteEntriesWithSubstring:substring];
return YES;
}
}
your_main_array:将是您从Web服务加载的原始数组。 autocomplete_array:将是搜索过程完成后您将获得的数组。
用户搜索时,您必须将autocomplete_array传递给UITableView