4
votes

I have a NSTableView as a very central part of my Application and want it to integrate more with the rest of it. It has only one column (it's a list) and I draw all Cells (normal NSTextFieldCells) myself.

The first problem is the highlighting. I draw the highlight myself and want to get rid of the blue background. I now fill the whole cell with the original background color to hide the blue background, but this looks bad when dragging the cell around. I tried overriding highlight:withFrame:inView: and highlightColorWithFrame:inView: of NSCell but nothing happened. How can I disable automatic highlighting?

I also want all rows/cells to be deselected when I click somewhere outside my NSTableView. Since the background / highlight of the selected cell turns gray there must be an event for this, but I can't find it. I let my cells expand on a double click and may need to undo this. So getting rid of the gray highlight is not enough.

EDIT: I add a subview to the NSTableView when a cell gets double clicked and then resignFirstResponder of the NSTableView gets called. I tried this:

- (BOOL)resignFirstResponder
{
    if (![[self subviews] containsObject:[[self window] firstResponder]])
    {
        [self deselectAll:self];
        ...
    }

    return YES;
}

Besides that it's not working I would need to implement this method for all objects in the view hierarchy. Is there an other solution to find out when the first responder leaves a certain view hierarchy?

3
Use setAction: method of NSOutlineView. Have a look at [this][1] answer. [1]: stackoverflow.com/a/23337627/944634Parag Bafna

3 Answers

2
votes

I wanted to achieve a similar solution (with an NSOutlineView but this has no difference): when clicking inside the outline view BUT not in a row with a cell (for instance at the empty bottom of a source list), I wanted the currently selected row to be deselected. I ended up with this little piece of code that might be of some help.

In a NSOutlineView subclass, I've put:

@implementation ClickOutlineView

- (void)mouseDown:(NSEvent *)theEvent
{
    NSPoint pointInWindow = [theEvent locationInWindow];
    NSPoint pointInOutlineView = [self convertPoint:pointInWindow toView:nil];

    int rowIndex = [self rowAtPoint:pointInOutlineView];

    if ([theEvent clickCount] == 1 && rowIndex == -1) {
        [self deselectAll:nil];
    }
    else {
        [super mouseDown:theEvent];
    }
}

@end
1
votes

No doubt it's too late to help the question's poster, but this may help others who come across this on Google (as I did).

You can prevent row selection by selecting 'None' in the 'Highlight' drop-down menu, in the attributes inspector in Xcode 4.

There is also an answer on SO here for setting this programmatically.

0
votes

Overloading -highlight:withFrame:inView: should be correct. How did you do the overload? Does an NSLog() statement indicate that your code is running? You should also look at NSTableView's -highlightSelectionInClipRect:, which may be more convenient for this.

In order to do something (such as unselect the current selection) when the user clicks outside the table view, overload -resignFirstResponder on NSTableView.