How to prevent NSSearchField from overwriting entered strings using the first autocompletion list entry?

安稳与你 提交于 2019-11-29 07:35:19

My own solution was actually very simple: just add the search string itself to the list of suggestions for the autocompletion.
This is done in the NSSearchField delegate method control:textView:completions:forPartialWordRange:indexOfSelectedItem::

...
partialString = [[textView string] substringWithRange:charRange];
...

matches       = [NSMutableArray array];

// find any match in our keyword array against what was typed -
for (i=0; i< count; i++)
{
string = [keywords objectAtIndex:i];
if ([string
     rangeOfString:partialString
     options: NSCaseInsensitiveSearch | NSForcedOrderingSearch
     range:NSMakeRange (0, [string length])]
    .location != NSNotFound) {
  [matches addObject:string];
 }
}
[matches sortUsingSelector:@selector(compare:)];

//  Make sure we insert the already entered string, even if it does not
//  match with any of the retrieved keywords. This will enter this string
//  in the search field, as we intended, and it will not be overwritten 
//  with any match.
[matches insertObject:partialString atIndex: 0];

return matches;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!