NSButton gets drawn inside custom NSCell, but is not actually clickable

半腔热情 提交于 2019-12-08 06:01:43

问题


I have a NSTableView which contains my custom NSCell subclass, IconCell.

The IconCell contains three elements : an image, text, and a button.

Here's a simplified version of my drawing code (closeButton is the button):

- (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
{
    NSPoint cellPoint = cellFrame.origin;

    [controlView lockFocus];

    CGFloat buttonWidth = [closeButton frame].size.width;

    [someNSImage drawInRect:NSMakeRect(cellPoint.x, cellPoint.y, ICON_WIDTH, ICON_HEIGHT) fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0 respectFlipped:YES hints:nil];
    [someNSString drawInRect:NSMakeRect(cellPoint.x+ICON_WIDTH+PADDING, cellPoint.y, cellFrame.size.width - ICON_WIDTH - buttonWidth, cellFrame.size.height) withAttributes:someTextAttributes];
    [(NSButtonCell*)[closeButton cell] drawWithFrame:NSMakeRect(cellPoint.x + cellFrame.size.width - buttonWidth, cellPoint.y, buttonWidth, cellFrame.size.height) inView:controlView];

    [controlView unlockFocus];
}

The drawing part works fine and produces something like the following:

which is what I want.

Moreover, I want one of two things to happen when the user interacts with the cell: if the user clicks anywhere on the cell, EXCEPT the close button, it should do actionA. If the user clicks on the close button, it should do actionB.

The problem I'm having is that the close button seems "invisible" -- if I click on it, it doesn't move (whereas a working button should show its pushed down state), and in general it behaves as if it wasn't there, and actionA is triggered instead of actionB.

This is how I set the two actions:

[tableView setAction:@selector(actionA)];

and

[closeButton setAction:@selector(actionB)];

What am I doing wrong?


回答1:


You're just drawing a picture of the button in the cell. This isn't the same thing as placing the actual button into the cell.

Cells aren't full views, so this is more complicated than you may think at first. If you really have to do this with cells, it's explained here: NSButtonCell inside custom NSCell.

But... if you can limit yourself to 10.7+, they've added view-based tableviews. These are much simpler, since you can put a full NSButton inside of your NSTableViewCellView. This is explained in the Table View Programming Guide. Highly recommended if you can limit yourself to 10.7+.



来源:https://stackoverflow.com/questions/9475814/nsbutton-gets-drawn-inside-custom-nscell-but-is-not-actually-clickable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!