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.
我有一个NSTableView,其中包含一些填充了数据的列。如何设置可以根据特定列的内容过滤行的搜索栏?我正在寻找在Finder中使用Spotlight或在iTunes中可以按元数据过滤歌曲的搜索栏中完成的事情类型。
2 个解决方案
#1
Can your data source be managed by NSArrayController?
您的数据源可以由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's arrangedObjects.name
key and bind the NSTableView's content to arrangedObjects
). You might want to read up on writing predicates.
有两种标准方法。第一种(pre-Tiger)技术是子类NSArrayController,重写arrangeObjects。第二个(后虎)是将NSSearchField的谓词绑定到NSArrayController的filterPredicate。在任何一种情况下,将视图属性绑定到控制器的arrangeObject键(例如,将“name”NSTableColumn绑定到控制器的arrangeObjects.name键并将NSTableView的内容绑定到arrangeObjects)。您可能希望阅读编写谓词。
#2
"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.
“Spotlight”功能实际上是通过SearchKit API提供给您的,尽管对于像您所描述的简单情况,NSPredicate可能是更好的选择。如果有人感兴趣,我为CocoaHeads写了一个SearchKit演示文稿,你可以在这里看看。
#1
Can your data source be managed by NSArrayController?
您的数据源可以由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's arrangedObjects.name
key and bind the NSTableView's content to arrangedObjects
). You might want to read up on writing predicates.
有两种标准方法。第一种(pre-Tiger)技术是子类NSArrayController,重写arrangeObjects。第二个(后虎)是将NSSearchField的谓词绑定到NSArrayController的filterPredicate。在任何一种情况下,将视图属性绑定到控制器的arrangeObject键(例如,将“name”NSTableColumn绑定到控制器的arrangeObjects.name键并将NSTableView的内容绑定到arrangeObjects)。您可能希望阅读编写谓词。
#2
"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.
“Spotlight”功能实际上是通过SearchKit API提供给您的,尽管对于像您所描述的简单情况,NSPredicate可能是更好的选择。如果有人感兴趣,我为CocoaHeads写了一个SearchKit演示文稿,你可以在这里看看。