UITextView中的自定义自动完成功能

时间:2013-01-10 06:11:27

标签: iphone autocomplete uitextview

我想为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:我可以使用的东西..如何传递数组中的值?

1 个答案:

答案 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