objective-c - NSTokenField 自动完成

标签 objective-c cocoa autocomplete parsing nstokenfield

我正在创建一个使用 NSTokenField 的应用程序。我需要自动完成。我正在使用 NSURLRequest 从 http://stackoverflow.com/filter/tags?_=<timestamp>&limit=6&q=<str_to_autocomplete>&timestamp=<timestamp> 请求数据

哪里<timestamp>是当前时间戳,<str_to_autocomplete>是要自动完成的字符串。所以,例如http://stackoverflow.com/filter/tags?_=1263657227137&q=lol&limit=6&timestamp=1263657227137

响应的格式如下:

javascript|23179
jquery|16936
sql-server|11768
ruby-on-rails|8669
best-practices|7037
ruby|6722

(数字是该标签的使用次数)。

我需要在 NSTokenField 下向用户提供一个包含此标签列表的列表,用户可以选择其中一个列表,也可以继续输入。

有人可以帮助我吗?谢谢。

编辑:我现在正在查看 Mac 开发中心。我应该使用这种方法吗:tokenField:completionsForSubstring:indexOfToken:indexOfSelectedItem:

最佳答案

这将发送给委托(delegate)来查询字符串数组:

tokenField:completionsForSubstring:indexOfToken:indexOfSelectedItem: 

这些字符串应该由 representedObject 中的 tokenField 处理(如果您只需要字符串,则不处理)。

tokenField 委托(delegate)中的示例:

- (NSArray *)tokenField:(NSTokenField *)tokenField completionsForSubstring:(NSString *)substring indexOfToken:(NSInteger)tokenIndex indexOfSelectedItem:(NSInteger *)selectedIndex
{
    //code to find the tags strings corresponding to substring (the string typed in the token)
    //then put them in an array (returnArray)
    return returnArray;
}

tokenField 将在您键入时在菜单中显示完成的字符串。所有详细信息都在文档中。

关于objective-c - NSTokenField 自动完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2077914/

相关文章:

iphone - 如何使 UITableView 中的单元格不变成蓝色?

objective-c - NSOutlineView显示三个层次

ios - 相当于 iOS 中的片段

ruby - RubyCocoa 应用程序中的 NULL?

macos - 如何找到哪个应用程序拥有模态窗口?

c# - 如何让 DataGridView AutoComplete 只显示一列?

ios - 重新排序单元格后如何更改 fetchresult Controller 数组中对象的顺序

cocoa - 搜索结果始终为 0(NSArray 和 NSPredicate)

android - 如何在 Android 的自定义微调器中设置搜索界面

javascript - Google 自动完成搜索栏默认值