Search code examples
swiftcocoanstableviewnsarraycontrollernssearchfield

SWIFT 3 OSX Cocoa Array Controller and CoreData Binding "Search on multiple columns"


I have a coredata entity bound to a tableView with 5 columns. I am using an array controller for managing the tableview sorting etc.

My issue is with searching using the bindings. I have a searchField bound to the ArrayController and the predicate format is set to the first column of data. Everything works fine. I can also change the predicate format in XCODE and search on a different column.

The problem I am having is knowing how the change the predicate format in my code to allow the user to search on a column of their choice...

I have attempted modifying the ArrayController as follows:

let predicate = NSPredicate(format: "projectNumber contains[cd] %@", searchBar.stringValue)
    searchArrayController.filterPredicate = predicate

AND

let predicate = NSPredicate(format: "customerName contains[cd] %@", searchBar.stringValue)
    searchArrayController.filterPredicate = predicate

projectNumber and customerName are attributes of my entity.

When I set the predicate this way the table data vanishes. I am able to get it to show again with fiddling of the search field but it ignores my predicate settings and only obeys the original one set in the XCODE storyboard.

StoryBoard setting is: Predicate Format = projectNumber contains[cd] $value

thank you in advance for any help you can provide...


Solution

  • Add a predicate binding for each column. When you bind predicate of the search field, a binding predicate2 appears. No coding required.