« Getting Started With NSTableView

Posted by Submission on February 09, 2002 [Feedback (9) & TrackBack (0)]

by H. Lally Singh

(website | email)

NSTableView provides an easy to use interface to show and edit tabular data. All you have to do is provide a source of data, and NSTableView will do all the work, asking that source for the data it needs as it needs it.


// Providing a Data Source

To be a data source for an NSTableView, you just have to implement three methods:

  • (int)numberOfRowsInTableView:(NSTableView *)aTableView - Returns the number of rows to display
  • (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(int)rowIndex - Returns data for a given row in a given column. This data is fed into the NSCell set up for that column.
  • (void)tableView:(NSTableView *)aTableView setObjectValue:(id)anObject forTableColumn:(NSTableColumn *)aTableColumn row:(int)rowIndex - Called when the user changes the value of a given data cell. Don't implement this if you want a read only NSTableView.

Using a category really makes alot of sense for many applications here. For example, if you have a single-column NSTableView, you can just use an NSArray to do all the work for you:

// Makes an NSArray work as an NSTableDataSource.
@implementation NSArray (NSTableDataSource)

// just returns the item for the right row
- (id)     tableView:(NSTableView *) aTableView
objectValueForTableColumn:(NSTableColumn *) aTableColumn
           row:(int) rowIndex
  return [self objectAtIndex:rowIndex];  

// just returns the number of items we have.
- (int)numberOfRowsInTableView:(NSTableView *)aTableView
  return [self count];  

It really is this easy. Just remember to call setDataSource: to tell the NSTableView who's providing the data, and call reloadData: whenever you update your data, so that NSTableView will redraw and show it!


// Customization

NSTableView is really not much more than a container for its columns, of class NSTableColumn. Each of these columns has two NSCell derivatives in it, one for the header and one for the data. Set each one using setHeaderCell: and setDataCell:.

By default, NSTableView uses an NSTextFieldCell as the cell for each column, which displays and lets the user edit text. For many applications, this is just perfect. However, you can make one column all NSButtonCells, NSImageCells or your own NSCell subclass.

Well, that's honestly about it. The documentation is pretty good on this topic, but sometimes a push start is the way to go. Enjoy Cocoa!


NSTableView method setBackgroundColor works only with the 2 columns "native" from IB. Is that a bug ? I wish I had a workaround to color uniformously, say, the 5 or 6 columns of an NSTableView (with text in it).
Looks like I should have to access the desobedient NSTextFieldCells but I don't know how to do that, being rather novice in Ojective-C.
Note : I have OS10.2.3 and pretty much enjoying cocoa

Thanks for advice !

Posted by: F. JOLY on February 6, 2003 05:48 AM

To solve your problem, regarding uniform tableView background colours, it was brought to my attention that I should copy and paste a column, within tableView. For example, if your current application has 4 columns in a tableView, highlight and delete the last two columns to the right. Then highlight the remaining second column, copy the column and, paste. The copied column will present itself to the right of the previous second column. Do it again for the fourth column. Your colours should now work properly in all four columns.

Posted by: jim k on February 15, 2003 01:08 AM

Sorry, I forgot to name the author of that advice....His name is Aaron Hillegass, author of "Cocoa Programming for Mac OSX".

Posted by: jim k on February 15, 2003 01:11 AM

anyone have a bare-bones example of using a pop-up in a table view (i.e. the Type column of the Outlets tab in IB, or the Class column in Property List Editor)? i've got it about 75%, but not quite there yet. not looking for it to be spelled out for me, just a little guidance would be smashing!


Posted by: jgregg on March 11, 2003 04:20 PM

I would like to do two things... Display an image in a column of a tableView (An example would be great, cause I've tried quite a few things).. and Two, format a text cell to display one string on one side of the cell, and another string on the other... Any help or examples would be very helpful.



Posted by: David Borton on April 3, 2003 06:31 PM

I have more than 1 NSTableView: tableView1, tableView2, ... how can I identify which is which? Since when I run the app, all tableViews show the same dataSource. Please help! Thanks alot.

Posted by: Jessica Tran on April 18, 2003 05:06 PM

There are several ways. Use different data sources. Pair each with its own controller. Compare pointers to the table views to see which one is sending a message. Etc. But use different data sources, at least.

Posted by: Erik J. Barzeski on April 18, 2003 05:46 PM

NSPopUpButtonCell * popup = [[[NSPopUpButtonCell alloc] init] autorelease];
[popup setBordered:NO];
[popup addItemWithTitle:@"1"];
[col setDataCell:popup];

Posted by: No on April 25, 2003 12:46 AM

How do you sort columns in NSTable? I'm quite a newbie so please be specific. A nice email will do me good. Thanx!

Posted by: George on May 2, 2003 05:26 AM
Post a comment