I wrote a view-based tableview, like this:
and I draw selection with NSTableRowView, code is like this:
- (void)drawRect:(NSRect)dirtyRect {
[[NSColor clearColor] setFill];
if (self.isClicked) {
[[[NSColor blackColor] colorWithAlphaComponent:0.08] setFill];
}
NSRect rowViewRect = NSMakeRect(0, 0, 274, 72);
NSBezierPath *path = [NSBezierPath bezierPathWithRect:rowViewRect];
[path fill];
}
But finally, I found the TableRowView is not over the tableView, so the selectedColor not cover the image and the button, it more like background color, but I need the selected TableRowView cover the view, just like this:
The selected color cover the image and the button. I have googled, but not found any ideas. Thanks for help~
So this is a bit tricky. The strategy is to have an overlay colored view with alpha less than 1 in your NSTableCellView and then add and remove it based on the cell's selection.
First, you need an NSView that can set a background color:
NSView_Background.h
NSView_Background.m
Second, in your NSTableCellView subclass, add a NSView_Background property:
Third, add this method to NSTableCellView subclass:
Fourth, add this to drawRect in your NSTableCellView subclass:
Finally, override NSTableCellView:setBackgroundStyle:
I know this seems hacky but this is the only way I could get this behavior. Hope this helps and good luck!