NSTableView & Multiple Columns


How would one go about making a table view with multiple columns? I’ve tried on my own, but I can’t figure out what to do with the NSTableColumn pointer that tableView:objectValueForTableColumn:row: passes.


There is a better way of doing what I’m about to tell you that uses bindings, which is discussed in a subsequent chapter. But the original way of handling multiple columns is by setting the “identifier” of each of the NSTableColumn objects. You do this in InterfaceBuilder using the Inspector.

Let’s say your columns are “First Name,” “Middle Name,” and “Last Name.” In the data object that you’re using as your model, you’ve probably got three attributes: first, middle, last. The easiest thing to do is to set the identifier for each of the columns to the same thing as your object’s attributes. So, for example, you click on Column 1 in InterfaceBuilder, find the “identifier” field in the Inspector, and name that identifier “first” – just like your object’s attribute. Do the same for columns 2 & 3, naming their identifiers “middle” and “last,” respectively.

Now, in your code (since you’re not using bindings) you would call:

NSString *key = [aTableColumn identifier];

You could then use key-value coding to get or set the data object’s attribute value as desired. The important (and convenient) thing is to use the same variable name for the column’s identifier and the corresponding attribute in the object you’re using to model the data. Hope this helps!