views:

215

answers:

2

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.

+2  A: 

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's arrangedObjects.name key and bind the NSTableView's content to arrangedObjects). You might want to read up on writing predicates.

outis
Keep in mind you don't need to use bindings to use NSPredicate, it works just as well on a plain NSArray object if you're using datasource methods to populate your table view.
Marc Charbonneau
+2  A: 

"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.

Marc Charbonneau