如何防止 NSSearchField 使用第一个自动完成列表条目覆盖输入的字符串?

发布于 2024-09-28 12:12:53 字数 323 浏览 3 评论 0原文

我正在寻找一种创建 nssearchfield 的方法,其行为如下:

  • 用户根据匹配项输入文本,
  • 自动完成下拉列表出现
  • 搜索字段中的文本自动完成到搜索中的第一项要点

是,我的字符串匹配搜索文本字段中的任何子字符串和自动完成功能将不起作用,因为它会覆盖我输入的字符串。事实上,这似乎应该是默认行为,还是我误解了搜索字段的目的?
进一步键入将进一步限制列表,但只有在自动完成下拉列表中选择一个项目后,该项目才会插入到文本字段中。

如果使用 nssearchfield 无法完成此操作,是否有其他选择?

I am looking for a way to create an nssearchfield that behaves as follows:

  • user types in text
  • based on matches an autocompletion drop-down appears
  • the text in the search field does not autocomplete to the first item in the list

The point is, my string matching searches for any substring and autocompletion in the text field would not work because it would overwrite my entered string. In fact it seems this should be the default behaviour, or am I misunderstanding the purpose of a search field?
Typing further would restrict the list further and further, but only after selecting an item in the autocompletion dropdown would that item be inserted into the text field.

If this cannot be accomplished using an nssearchfield, is there an alternative?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清晨说晚安 2024-10-05 12:12:53

我自己的解决方案实际上非常简单:只需将搜索字符串本身添加到自动完成的建议列表中即可。
这是在 NSSearchField 委托方法 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;

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文