如何获得类似于 Spotlight 或 iTunes 的搜索功能?
我有一个 NSTableView,其中包含一些填充数据的列。 如何设置可以根据特定列的内容过滤行的搜索栏? 我正在寻找使用 Finder 中的 Spotlight 或 iTunes 中的搜索栏可以按元数据过滤歌曲的类型。
I have an NSTableView that contains a few columns that are populated with data. How do I set up a search bar that can filter the rows based on the content of a specific column? I am looking for the type of thing that is done with Spotlight in the Finder or the search bar in iTunes that can filter songs by metadata.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
“Spotlight”功能实际上是通过 SearchKit API 为您提供的,尽管对于像您所描述的简单情况, NSPredicate 可能是更好的选择。 如果有人感兴趣,我为 CocoaHeads 编写了一个 SearchKit 演示文稿,您可以获取看看这里。
"Spotlight" capability is actually given to you with the SearchKit API, although for a simple case like what you're describing NSPredicate would probably be a better choice. If anyone's interested though, I wrote a SearchKit presentation for CocoaHeads you can take a look at here.
你的数据源可以由 NSArrayController 管理吗?
有 2 种标准方法。 第一种(Tiger 之前的)技术是子类化 NSArrayController,覆盖 ArrangeObjects。 第二个(Tiger 之后)是将 NSSearchField 的谓词绑定到 NSArrayController 的 filterPredicate。 无论哪种情况,都将视图属性绑定到控制器的
arrangedObject
键(例如,将“name”NSTableColumn 绑定到控制器的arrangedObjects.name
键,并将 NSTableView 的内容绑定到排列对象
)。 您可能想阅读编写谓词。Can your data source be managed by NSArrayController?
There are 2 standard approaches. The first (pre-Tiger) technique is to subclass NSArrayController, overriding arrangeObjects. The second (post-Tiger) is to bind NSSearchField's predicates to the NSArrayController's filterPredicate. In either case, bind your view properties to the controller's
arrangedObject
key (e.g. bind the "name" NSTableColumn to the controller'sarrangedObjects.name
key and bind the NSTableView's content toarrangedObjects
). You might want to read up on writing predicates.