0

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

4

1 回答 1

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

于 2013-01-10T06:21:22.650 回答