Pregunta

I have a NSTableView tied to a NSArrayController. The first column are checkboxes. I want to have the users select the rows to perform an action on via the checkboxes and when the user clicks the "Run" button I want to set the state of that column to be un-editable.

How do I go about this?

Thanks!

¿Fue útil?

Solución

rather than disabling the column I disable the individual checkbox cells in my app based on a global app state.

Preconditions:

1.) your "Run button" sets some sort of a global state in your controller class. For me its a "Play" button that sets a member in my controller like:

    if ([self gotoNextSong])
    _currentPlayMode = PLAYMODE_PLAY;
else
    _currentPlayMode = PLAYMODE_STOP;

2.) Your controller is already a delegate to the NSTableView. If not: In your MyController.h:

    @interface MyController : NSObject <NSTableViewDelegate>

In your MyController.m make your controller to a delegate of the table - e.g. in your init: method

    [self.myTableView setDelegate:self];

3.) You know the identifier of the column that holds the checkboxes. If not, either its the automatic name like column1, column2, ... or you can set it in Interface Builder while the column is active in Identity Inspector / Identity / Identifier. In my case the column name is "checkboxes"

Solution:

1.) Add a delegate method for the table to inform you when the cells are rendered. Inside this method enable or disable the cells of the checkbox-row as desired

- (void)tableView:(NSTableView *)aTableView willDisplayCell:(id)aCell forTableColumn:  (NSTableColumn *)aTableColumn row:(int)rowIndex
{
    if(([[aTableColumn identifier] isEqualToString:@"checkboxes"]))
    {
        if (_currentPlayMode == PLAYMODE_PLAY)
            [aCell setEnabled:NO];
       else
            [aCell setEnabled:YES];
    }
}

2.) Make sure that your RUN-button not only sets the global state (in my case _currentPlayMode), but also force the NSTableView to redraw afterwards:

[self.myTableView setNeedsDisplay:YES];
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top