NSButton在自定义NSCell内部绘制,但实际上不可点击 [英] NSButton gets drawn inside custom NSCell, but is not actually clickable

查看:654
本文介绍了NSButton在自定义NSCell内部绘制,但实际上不可点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个NSTableView,其中包含我的自定义NSCell子类IconCell。



IconCell包含三个元素:图像,文本和按钮。

这是我的绘图代码的简化版本( closeButton 是按钮):

   - (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操作: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];
}

绘图部分工作正常,并产生如下内容:





这是我想要的。



此外,当用户与单元格交互时,我想要两个事情之一:单元格,除了关闭按钮,它应该做 actionA 。如果用户点击关闭按钮,它应该做 actionB



我遇到的问题是关闭按钮似乎不可见 - 如果我点击它,它不移动(而工作按钮应该显示它的下压状态),一般来说,它的行为,如果它不在那里,和 actionA 被触发,而不是 actionB



两个动作:

  [tableView setAction:@selector(actionA)]; 

  [closeButton setAction:@selector(actionB)]; 

我做错了什么?

解决方案

您只是在单元格中绘制按钮的图片。这与将实际的按钮放入单元格不一样。



单元格不是完整的视图,因此这比你想象的更复杂。如果你真的需要这样做与单元格,这里解释: NSButtonCell自定义NSCell内。 / p>

但是...如果你可以限制自己为10.7+,他们已经添加了基于视图的tableview。这些简单得多,因为你可以在 NSTableViewCellView 里面放置一个完整的 NSButton 。有关说明请参阅表视图编程指南。强烈推荐,如果你可以限制自己到10.7 +。


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?

解决方案

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+.

这篇关于NSButton在自定义NSCell内部绘制,但实际上不可点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆