Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to handle multiple NSTableView(s)

Tags:

cocoa

What is considered the best way of handling multiple NSTableViews without using Cocoa Bindings?

In my app, I have two NSTableViews that are sufficiently closely related that I'm using the same object as the delegate and dataSource for both. The problem is both tableViews invoke the same methods. I currently discriminate between the two tableViews on the basis of NSControl -tag.

The deeper I get into this code, the uglier the use of -tag looks. I end up creating largely duplicate code to distinguish between the tableViews in each delegate/dataSource method. The code ends up being distinctly non-object oriented.

I could create a separate object to handle one or the other tableView, but the creation of said object would be a largely artificial construct just to provide a distinct delegate/dataSource.

Is everyone just using Cocoa Bindings now? I'm avoiding Bindings as I would like to hone my Cocoa skills on techniques that are transferrable between Mac OS and iPhone.

like image 719
Jay O'Conor Avatar asked Feb 02 '09 21:02

Jay O'Conor


1 Answers

Every delegate/dataSource method for NSTableView passes the instance of NSTableView that's calling it as the first parameter (except for the ones that pass NSNotification objects, in which case the NSNotification's object is the table view instance). Some examples include:

- (int)numberOfRowsForTableView:(NSTableView*)aTableView;

- (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn*)aTableColumn row:(NSInteger)rowIndex

- (void)tableViewSelectionDidChange:(NSNotification *)aNotification

If you're using one controller object as a delegate/data source for multiple tables, you can just use that parameter to differentiate between them.

like image 169
Brian Webster Avatar answered Dec 23 '22 12:12

Brian Webster