Swift NSSearchField NSTableView With Bindings

Swift NSSearchField NSTableView With Bindings

Created By: Debasis Das (may 2016)

In this post we are going to create a simple cocoa application having a NSSearchField and bind the same to a NSTableView Array Controller

Step 1 : Create a new Cocoa Application from the xcode new project templates

NSSearchField NSTableView With Bindings 1

Step 2: Create a new project

NSSearchField NSTableView With Bindings 2

Step 3: Add a NSSearchField and a NSTableView to the window.

The NSTableView instance will be a cell based table view having 2 table columns (First Name, Last Name with identifiers firstName and lastName for the columns)

NSSearchField NSTableView With Bindings 3

Step 4: Create a Person Class that will represent each record in the table view

class Person:NSObject{
    var firstName:String = ""
    var lastName:String = ""
    class func createPerson(fName:String, lName:String)->Person{
        let person = Person()
        person.firstName = fName
        person.lastName = lName
        return person

Step 5: Add a dataArray function in the App Delegate which returns an array of Person Objects

    func dataArray()->NSMutableArray{
        let arr = NSMutableArray()
        arr.addObject(Person.createPerson("Debasis", lName: "Das"))
        arr.addObject(Person.createPerson("John", lName: "ccdd"))
        arr.addObject(Person.createPerson("Jane", lName: "eeff"))
        return arr

Step 5: Add a NSArrayController instance to the xib and bind the contentArray to dataArray function of the App Controller/ Delegate

NSSearchField NSTableView With Bindings 10


Step 6: After binding the NSArrayController to the dataArray function, bind the table columns to the ArrayController->arrangedObjects ->keyPaths

NSSearchField NSTableView With Bindings 5

Step 7: Bind the NSSearchField for the search predicate with the Array Controller with controller key as filterPredicate and Predicate Format as “self.firstName contains[cd] $value OR self.lastName contains[cd] $value”


NSSearchField NSTableView With Bindings 9

Step 8: Build and Run



NSSearchField NSTableView With Bindings 6


Step 9: Type in different search values in the search field and see the NSSearchField working along with the NSTableView to render only filtered records/Persons

NSSearchField NSTableView With Bindings 8 NSSearchField NSTableView With Bindings 7

You can download the working project here NSSearchField_NSTableView_WithBindings



Posted in Cocoa, Swift Tagged with: , ,
2 comments on “Swift NSSearchField NSTableView With Bindings
  1. Thomas says:

    Xcode 7.3.1 lists MainMenu.xib objects differently than the version you are using. Yours shows Placeholders, Objects and Window as scenes. 7.3.1 lists these as Application Scene, Window Controller Scene and View Controller Scene.

    I am unable to follow your instructions in 7.3.1 and get the table view to show the data. Any idea’s how I can get things to work in 7.3.1?


    • Debasis Das says:

      Hi Thomas,

      You are using Storyboards.
      Try creating a new application and donot check the storyboard checkbox in the new project options and you should be able to follow the steps.
      Or you can simply copy the view contents of the main menu.xib and paste it in the view controller scene and then write the code in the ViewController.swift file. That too should work. Let me know if that works for you or I will create an app in Storyboard and share

      Best Regards

1 Pings/Trackbacks for "Swift NSSearchField NSTableView With Bindings"
  1. […] 1 http://www.knowstack.com/swift-nssearchfield-nstableview-with-bindings/ In this article we had achieved the same functionality using bindings. This approach is the […]

Leave a Reply

Your email address will not be published. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Recent Posts

Hit Counter provided by technology news