Morpheus
Morpheus

Reputation: 3523

Two NSTableViews in a single view

I have two NSTableViews in a same view and they have the same data source and delegate.

The two table views are named as varTableView and constraintTableView. Now the number of rows in each is different and I I am finding it difficult to implement the two methods:

-(NSInteger) numberOfRowsInTableView:(NSTableView *)tableView -(id) tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row

Is there any way to identify the tableviews and return the corresponding number of rows?

My implementation was something like this:

-(NSInteger) numberOfRowsInTableView:(NSTableView *)tableView{
    if([tableView.identifier isEqualToString:@"variableTableView"]){
        return [self.arrayOfVariableNames count];

    }
    else{
        return [self.arrayOfConstraintNames count];
    }
}

it is always returning the count of constraints names but not the variable names

Upvotes: 0

Views: 217

Answers (3)

GoodSp33d
GoodSp33d

Reputation: 6282

As suggested in other answers you can differentiate between two tables by comparing its outlets. There s also another way but you would have to change your existing designs a bit.

Instead of adding two table views in one view controller, create a placeholder view for both of them. Create two table view controllers and add them to these placeholder views as subviews. This way you can have both tables in separate controllers and it will get rid of all the if else blocks which you will end up writing. It might slow you down initially but you will get benefitted later.

Upvotes: 1

Sheshnath
Sheshnath

Reputation: 3393

you can implement this either by using name of table or tag of table to know for which table particular delegate method is called for like

   -(NSInteger) numberOfRowsInTableView:(NSTableView *)tableView -(id) tableView:  (NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:   (NSInteger)row
   {
         if (tableView == varTableView)// or check table tag, first you need to set tag for table
         {
           // do something with varTableView
           return ([array1 count]);

        }
       else if (tableView == constraintTableView)
       {
        // do something with constraintTableView
          return ([array2 count]);
       }
   }

Upvotes: 3

Extra Savoir-Faire
Extra Savoir-Faire

Reputation: 6036

Yes. It's the tableView parameter in the methods you included.

if (tableView == varTableView)
{
    // do something with varTableView
}
else if (tableView == constraintTableView)
{
    // do something with constraintTableView
}

The number of rows tends to be the number of objects in the array serving as the data source.

Upvotes: 3

Related Questions