0
votes

I have an NSTableview in view based mode, in it I have custom NSTableViewCells. Each cell has a button, but I only want the button to be displayed if the cell is selected. So when the cell becomes deselected I want to hide the button. I have tried to iterate through each row unsuccessfully and am unsure as to how to go about this.

This is what I'm doing to show the button

-(void)tableViewSelectionDidChange:(NSNotification *)notification
{
    ...

    //Hide / show dropdown menus
    NSInteger selected = [_tableView selectedRow];
    CustomCell *cell = [_tableView viewAtColumn:0 row:selected makeIfNecessary:NO];
    [cell setIsMenuHidden:NO];

}
2
reload table after doing your code... might work...Fahim Parkar
Sorry that doesn't workBON

2 Answers

0
votes

Managed to get it working, it wasn't working before as I was setting the button as hidden in the -viewForTableColumn:Row method! Would still like to know if there is a better way to do this :)

//Hide / show dropdown menus
NSInteger selected = [_tableView selectedRow];
for(int i = 0; i < [_tableView numberOfRows]; i++) {
    if(i == selected) {
        [[_tableView viewAtColumn:0 row:i makeIfNecessary:NO] setIsMenuHidden:NO];
    } else {
        [[_tableView viewAtColumn:0 row:i makeIfNecessary:NO] setIsMenuHidden:YES];
    }

}
0
votes

In your custom cell view, you can override -viewWillDraw. Call through to super and then make whatever adjustments based on the state that you want.

You can use the table view's -rowForView: and -isRowSelected: methods to determine whether to hide or show the button. Alternatively, the cell view's superview will be the row view and NSTableRowView has a selected property (-isSelected getter).