gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 16:11:23 26 4
gpt4 key购买 nike

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

  • 用户输入文本
  • 根据匹配结果出现自动完成下拉列表
  • 搜索字段中的文本不会自动填充到列表中的第一项

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

如果使用 nssearchfield 无法完成此操作,是否有替代方案?

最佳答案

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

关于cocoa - 如何防止 NSSearchField 使用第一个自动完成列表条目覆盖输入的字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3981439/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com