Search code examples
objective-ccocoanstableviewnstablecolumn

How to set second column text of NSTableView?


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.


Solution

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