Search code examples
objective-ccocoanstableviewappkit

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


Solution

  • 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]);
           }
       }