Saturday, July 26, 2008

NSTableView - with bindings

I was having trouble using bindings with an NSTableView, but then I found a tutorial on the web written by Bob Ippolito (pdf). It's relatively old (2004) but it still works. I followed the instructions, and this is what I got.



Only one function is needed (in the AppDelegate):

    def passwords(self):
return( [ {'name':'Larry', 'uid':1},
{'name':'Moe', 'uid':2},
{'name':'Curly', 'uid':3} ] )

The bindings are as follows:
Array Controller => contentArray => File's Owner
Model Key Path = "delegate.passwords"

Table Columns => Value => Array Controller
controllerKey = arrangedObjects
Model Key Path = "name" or "uid"

One tricky part when dealing with an NSTableView is to know what is actually selected currently. What I do is bring up an Inspector window, then the name of that window will change to reflect the value of the current selection. For example, when you click on an NSTableView, it will cycle through Scroll View, Table View, or Table Column if you click in the body, or Table Header View if you click in the header. Be sure that when you set the binding, you're in Table Column.

One problem with this solution is that it's a one-way binding. My next step will be to figure out how to get the model to reflect editing in the Table View, and how to get changes to the model reflected in the Table View.

I did test that we can bind to the AppDelegate with a Model Key Path of "passwords." When we use File's Owner, the message is sent to the instance of NSApplication, which forwards it to its delegate, and we end up in the same place. I'm not sure whether this makes any difference.

No comments: