0

I've encountered a problem while creating an NSTableView with 2 columns.

How do you set the value of the a row's corresponding column? I would like to have colText to be in the row's column, but it seems nearly impossible to achieve.

Here's the code:

 - (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(int)rowIndex {
    uint32_t size = *((uint32_t *)[[itemSizes objectAtIndex:rowIndex] bytes]);
    NSString *colText = [NSString stringWithFormat:@"%d bytes", size];

    return [objNames objectAtIndex:rowIndex];
}

I would like [objNames objectAtIndex:rowIndex] to be in the left column, which it is, and colText on the right?

I'm a bit stuck.

Any help appreciated.

Jack Greenhill
  • 10,240
  • 12
  • 38
  • 70

3 Answers3

2

Give the columns identifiers (e.g. @"firstColumnID" and @"secondColumnID") so you can do:

- (id)tableView:(NSTableView *)aTableView 
      objectValueForTableColumn:(NSTableColumn *)aTableColumn 
      row:(NSInteger)rowIndex
{
    if ([[aTableColumn identifier] isEqualToString: @"firstColumnID"])
        return [objNames objectAtIndex: rowIndex];
    else 
    {
        // In the original code, NSData was used, but for simple values
        // like this, NSNumbers are better suited, IMO:
        NSUInteger size = [[itemSizes objectAtIndex: rowIndex] unsignedIntegerValue];
        return [NSString stringWithFormat: @"%lu bytes", size];
    }
}
Rudy Velthuis
  • 28,387
  • 5
  • 46
  • 94
  • Worth pointing out: You've also changed the interpretation of `itemSizes` from an array of NSData objects to an array of NSNumber objects. A virtuous change that must be reflected elsewhere in the code (i.e., wherever the array is populated). – Peter Hosey Aug 20 '11 at 07:48
  • @Peter: Thanks, I always mix these up. – Rudy Velthuis Aug 20 '11 at 07:50
  • @Peter: Oops. I guess I'll change it back or add a comment in the code. – Rudy Velthuis Aug 20 '11 at 07:51
0

Another option, with less coding and without bindings is to set the column identifiers to be the same as class properties. You can dispense with the if/else in that case.

there is more on this topic here:

NSTableView with multiple columns

Community
  • 1
  • 1
Daniel Saban
  • 478
  • 3
  • 16
0

Two options:

  • Update your code and user interface to use bindings: add a method -formattedSize and then add an NSArrayController to the NIB, bind the array controller to your data array, and then bind the table column to arrangedObjects.description and arrangedObjects.formattedSize.

  • or use NSTableColumn to differentiate the data returned.

e.g.

- (id)tableView:(NSTableView *)tableView
      objectValueForTableColumn:(NSTableColumn *)tableColumn
      row:(int)row
{
    id value = nil;

    if ([[tableColumn identifier] isEqualToString:@"column1"])
    {
        value = [arrayA objectAtIndex:row];
    }
    else
    {
        value = [arrayB objectAtIndex:row];
    }

    return value;
}
ioquatix
  • 1,411
  • 17
  • 32