Back to Index
Getting an NSTableView to
The problem with an NSTableView is that we cannot tell it what to
do. NSTableView works like the old joke about Soviet Russia: "In
Soviet Russia television watches you". In other words, instead of us
calling methods in the NSTableView object, the NSTableView object
expects us to provide a data source object with methods for the
NSTableView object to call.
Our plan shall be to put an NSTableView in our window and then write
a class that acts as its data source, instantiate the class and hope
for the best.
1. Double-click MainWindow.xib to open Xcode.
2. In Xcode, find the main window. It's the big thing that looks
like a window.
3. In the Object Library find an NSTableView control.
Picture 1: We found an NSTableView in the Object Library
4. Drag the NSTableView into the window.
Note that the NSTableView is embedded in an NSScrollView which we
will try to ignore.
Picture 2: The window with an NSTableView
5. Try clicking on the actual NSTableView control.
6. Find the configuration thingy for the NSTableView control and
configure it for one column, with no reordering or resizing.
Picture 3: We managed to click on the actual NSTableView control.
Picture 4: The NSTableView configuration thingy.
7. Find the button to call the Assistant editor.
8. Configure the Assistant editor to display MainWindow.h and
control-drag the NSTableView control into the MainWindow class.
Picture 5: Found the Assistant editor button.
Picture 6: Name the outlet "tblTable".
9. Add a file TableViewDataSource.cs with the following content.
public class TableViewDataSource : NSTableViewDataSource
public TableViewDataSource ()
// This method will be called by the
NSTableView control to learn the number of rows to display.
public int NumberOfRowsInTableView(NSTableView
// We just return a static
2. We will have two rows.
// This method will be called by the control
for each column and each row.
ObjectValueForTableColumn(NSTableView table, NSTableColumn col,
will write "Hello" in the first row...
"World" in the second.
that NSTableView requires an NSString, which we create with new
need a default value.
10. Modify MainWindow.cs to include the following.
// This method will be called automatically when the main window
public override void AwakeFromNib()
tblTable.DataSource = new
MainWindow.cs knows about tblTable. All we have to do to connect our
NSTableView control to our data source is to tell tblTable that its
data source is a new instance of TableViewDataSource.
11. Run the application and see that "Hello" and "World" are being
displayed in the NSTableView.
Picture 7: Result!
(c) Andrew J. Brehm
Feel free to copy/translate/whatever but keep a pointer to the
original location in case I find one of my many, many mistakes and
update the document.
Zurich, January 23rd 2012