Best way to handle multiple NSTableViews

What is considered the best way to handle multiple NSTableViews without using Cocoa Bindings?

In my application, I have two NSTableViews that are closely related to the fact that I use the same object as the delegate and dataSource for both. The problem is that tableViews reference the same methods. I am currently distinguishing between two table views based on NSControl -tag.

The deeper I get into this code, the more fun it will be to use -tag. I end up creating mostly duplicate code to distinguish between tableViews in each delegate / dataSource method. The code ends up explicitly non-object oriented.

I could create a separate object to handle one or the other tableView, but creating the specified object would be pretty much an artificial construct to provide a separate delegate / data source.

Does everyone just use Cocoa Bindings now? I avoid Bindings because I would like to hone my Cocoa skills with the methods that are passed between Mac OS and iPhone.

+6
cocoa
source share
3 answers

Each delegate method / dataSource for NSTableView passes an instance of NSTableView that calls it as the first parameter (except for those that pass NSNotification objects, in which case the NSNotification object is an instance of the table). Here are some examples:

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

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

- (void)tableViewSelectionDidChange:(NSNotification *)aNotification

If you use one controller object as the delegate / data source for multiple tables, you can simply use this parameter to distinguish between them.

+10
source share

for the method:

 - (void)tableViewSelectionDidChange:(NSNotification *)aNotification 

you can use:

 NSTableView *theTable = (NSTableView *)[aNotification object]; if(theTable==listeDesMots) ... 
+3
source share

It looks like you should use a different delegate object for each view, but the same data source. In other words, one model for separate view and controller objects.

I donโ€™t think this is an artificial difference because the objects have quite different goals, but you want to use the same data. The biggest rule that you are breaking now is that each object should have one goal. The goal of each object can be to search and display data in a specific way.

Good luck

+2
source share

All Articles