tags:

views:

281

answers:

2

What I basically trying to achieve is to change the formatting of a cell in a table to blank it out if the cell is not enabled.

To define whether a cell in a table column is enabled I've bound using the enabled binding and this is working fine, enabling the cells in some rows of the table and not in others. I have defined a sub-class of NSTextFieldCell that I am using to change the format of the cell. This also appears to be working fine, but I am having trouble triggering the change depending on whether the cell in the table is enabled. Originally I tried to activate the switch by calling [self isEnabled] on the cell. This worked but the switch never activated, which I assume is because the original binding defining whether a cell is enabled was to the table column rather than to the cell.

Does anyone have a method to easily achieve this. The only method I have noticed would be to sub-class the NSTableColumn and write a custom version of dataCellForRow: but this somehow doesn't seem like the best way to solve this problem.

+1  A: 

I use the table view's delegate method to set the text color before displaying a cell. You could set the cell color to the background color to make it invisible when the cell is disabled.

- (void)tableView:(NSTableView *)aTableView willDisplayCell:(id)aCell forTableColumn:(NSTableColumn *)aTableColumn row:(int)rowIndex
{
    NSColor* theColor = [NSColor blackColor];
    BOOL enable = YES;
    if(![self tableView:aTableView shouldSelectRow:rowIndex])
    {
      theColor = [aCell backgroundColor];
      enable = NO;
    }

    [aCell setTextColor:theColor];
    [aCell setEnabled:enable];
}

I tested this code briefly and it worked. I am not using bindings, but that shouldn't matter for this as long as the table's delegate is set up properly.

Mark Thalman
+1  A: 

Thankyou for your answer Mark. It didn't seem to work for me but it did point me in the right direction. I can't quite do everything I want because filling a cell with a background colour is a little harsh with the square edges but it works fairly robustly so I'll use it for now. The actually code I used is:

- (void)tableView:(NSTableView *)aTableView 
  willDisplayCell:(id)aCell 
  forTableColumn:(NSTableColumn *)aTableColumn 
    row:(int)rowIndex
{
    NSColor *cellColor = [NSColor lightGrayColor];
    if([aCell isEnabled])
    {
     cellColor = [NSColor whiteColor];
    }
    if([aCell isKindOfClass:[NSTextFieldCell class]])
    {
     [aCell setDrawsBackground:YES];
    }
    [aCell setBackgroundColor:cellColor];
}
Ian Turner