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