rickcnagy
rickcnagy

Reputation: 1824

Cocoa - multiple view-based NSTableViews

I am just beginning to teach myself cocoa, and I am running into a (probably simple) issue displaying multiple view-based NSTableViews with same delegate and controller (the App Delegate, in my case). I saw this post: Best way to handle multiple NSTableView(s) but the method described still gives me errors - specifically

Duplicate declaration of method 'numberOfRowsInTableView:' Duplicate declaration of method 'tableView:viewForTableColumn:row:'

Obviously, the compiler isn't seeing that the different method declarations are for different table views.

The code for the tableviews in the AppDelegate.m file is

@synthesize tableView1;
@synthesize tableView2;

-(NSUInteger)numberOfRowsInTableView:(NSTableView *)tableView1
{
    return 1;
}

-(NSUInteger)numberOfRowsInTableView:(NSTableView *)tableView2
{
    return 2;
}

- (NSView *)tableView:(NSTableView *)tableView1 viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
    NSTableCellView *resultForTable1 = [tableView1 makeViewWithIdentifier:tableColumn.identifier owner:self];
    resultForTable1.textField.stringValue = @"This should appear in the first tableView";
    return resultForTable1;
}

- (NSView *)tableView:(NSTableView *)tableView2 viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
    NSTableCellView *resultForTable2 = [tableView2 makeViewWithIdentifier:tableColumn.identifier owner:self];
    resultForTable2.textField.stringValue = @"This should appear in the second tableView";
    return resultForTable2;
}

and in my AppDelegate.h file, I have:

@property (weak) IBOutlet NSTableView *tableView1;
@property (weak) IBOutlet NSTableView *tableView2;

What am I doing wrong here?

Upvotes: 3

Views: 1409

Answers (2)

NSGod
NSGod

Reputation: 22948

I think you're misunderstanding the method described in that answer.

You're getting a compiler error because you are trying to implement the same method twice. The following would all be implementations of the same method:

- (void)setBlah:(id)aBlah {
- (void)setBlah:(id)newBlah {
- (void)setBlah:(id)theNewBlah {

The different "names" given to the parameter that follows the (id) parameter type are only local to the implementation block of that method.

You should be able to accomplish what you want to do using code like the following:

@synthesize tableView1;
@synthesize tableView2;

- (NSUInteger)numberOfRowsInTableView:(NSTableView *)aTableView {
    if (aTableView == tableView1) return 1;
    else if (aTableView == tableView2) return 2;
    return 0;
}

- (NSView *)tableView:(NSTableView *)aTableView
     viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {

    NSTableCellView *tableCellView = [aTableView
                   makeViewWithIdentifier:tableColumn.identifier owner:self];
    if (aTableView == tableView1) {
        tableCellView.textField.stringValue = 
                 @"This should appear in the first tableView";
    } else if (aTableView == tableView2) {
        tableCellView.textField.stringValue = 
                 @"This should appear in the second tableView";
    }
    return tableCellView;
}

Notice that I made sure to name the parameter aTableView, something different from the instance variables, so that I can successfully compare it to the instance variables in the following lines.

Upvotes: 4

rdelmar
rdelmar

Reputation: 104082

You don't duplicate the methods multiple times -- you're not supplying the argument, "tableView1", tableView2", etc., the table views call these methods and send themselves as the argument. So, if you want to use the same delegate for multiple tables, you put if statements in the delegate methods, to ascertain which table sent the message. Declare an IBOutlet for each table and then do (in pseudo code) if table1 .... else if table2 ...etc.

Upvotes: 1

Related Questions