使用NSPopUpButtonCell时,在NSTableView中获取重复的标题按钮单元格 [英] Getting duplicate header button cell in NSTableView when using NSPopUpButtonCell
问题描述
我有一个动态NSTableView,可以添加许多列,具体取决于提供的数据。对于每一列,我已将标题单元格设置为NSPopUpButtonCell。 (边注:我不得不使用自定义子类类为NSTableHeaderView否则菜单不弹出)。除了右上方的重复或额外的标题按钮单元格,所有效果都很好。它完美地反映了之前的列选择,如截图所示。我的问题是如何停止NSTableView回收以前的弹出标题单元格? (顺便说一下,我试过setCornerView方法,但只影响垂直滚动条上的标题区域。)
I have a dynamic NSTableView which can add a number of columns depending on the data provided. For each column I have set the header cell to be a NSPopUpButtonCell. (Side-note: I've had to use a custom subclass class for NSTableHeaderView otherwise the menu doesn't pop-up). All works well, apart from a duplicate or extra header button cell on the top right. It mirrors perfectly the previous column selection as shown in screenshots. My question is how do I stop the NSTableView from recycling the previous popup header cell? (By the way I have tried the setCornerView method but that only effects the header area above the vertical scrollbar.)
推荐答案
同样的问题。我用快速修复,
I came across the same problem this week. I went with the quick fix,
[_tableView sizeLastColumnToFit];
(但是,与OP讨论后,需要在标题中使用NSPopUpButtonCell的子类, NSTableHeaderView。我在下面附上我的解决方案)
(However, after discussion with OP this requires that you use a subclass of NSPopUpButtonCell in the header and also NSTableHeaderView. I attach my solution below)
您可以通过组合这里概述的方法来实现
You can to this by combining the approaches outlined here,
- PopUpTableHeaderCell
- DataTableHeaderView
这是一个简化的代码片段,
Here is a simplified snippet,
// PopUpTableHeaderCell.h
#import <Cocoa/Cocoa.h>
/* Credit: http://www.cocoabuilder.com/archive/cocoa/133285-placing-controls-inside-table-header-view-solution.html#133285 */
@interface PopUpTableHeaderCell : NSPopUpButtonCell
@property (strong) NSTableHeaderCell *tableHeaderCell; // Just used for drawing the background
@end
// PopUpTableHeaderCell.m
@implementation PopUpTableHeaderCell
- (id)init {
if (self = [super init]){
// Init our table header cell and set a blank title, ready for drawing
_tableHeaderCell = [[NSTableHeaderCell alloc] init];
[_tableHeaderCell setTitle:@""];
// Set up the popup cell attributes
[self setControlSize:NSMiniControlSize];
[self setArrowPosition:NSPopUpNoArrow];
[self setBordered:NO];
[self setBezeled:NO];
[self setFont:[NSFont systemFontOfSize:[NSFont smallSystemFontSize]]];
}
return self;
}
// We do all drawing ourselves to make our popup cell look like a header cell
- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView*)controlView{
[_tableHeaderCell drawWithFrame:cellFrame inView:controlView];
// Now draw the text and image over the top
[self drawInteriorWithFrame:cellFrame inView:controlView];
}
@end
现在为NSTableViewHeader子类。
Now for the NSTableViewHeader subclass.
//DataTableHeaderView.h
#import <Cocoa/Cocoa.h>
/* Credit: http://forums.macnn.com/79/developer-center/304072/problem-of-nspopupbuttoncell-within-nstableheaderview/ */
@interface DataTableHeaderView : NSTableHeaderView
@end
//DataTableHeaderView.m
#import "DataTableHeaderView.h"
/* Credit: http://forums.macnn.com/79/developer-center/304072/problem-of-nspopupbuttoncell-within-nstableheaderview/ */
@implementation DataTableHeaderView
- (id)initWithFrame:(NSRect)frame {
self = [super initWithFrame:frame];
if (self) {
// Initialization code here.
}
return self;
}
- (void)mouseDown:(NSEvent *)theEvent {
// Figure which column, if any, was clicked
NSPoint clickedPoint = [self convertPoint:theEvent.locationInWindow fromView:nil];
NSInteger columnIndex = [self columnAtPoint:clickedPoint];
if (columnIndex < 0) {
return [super mouseDown:theEvent];
}
NSRect columnRect = [self headerRectOfColumn:columnIndex];
// I want to preserve column resizing. If you do not, remove this
if (![self mouse:clickedPoint inRect:NSInsetRect(columnRect, 3, 0)]) {
return [super mouseDown:theEvent];
}
// Now, pop the cell's menu
[[[self.tableView.tableColumns objectAtIndex:columnIndex] headerCell] performClickWithFrame:columnRect inView:self];
[self setNeedsDisplay:YES];
}
- (BOOL)isOpaque {
return NO;
}
- (void)drawRect:(NSRect)dirtyRect {
[super drawRect:dirtyRect];
// Drawing code here.
}
@end
AppDelegate -awakeFromNib
或类似,
You can tie everything together in the AppDelegate -awakeFromNib
or similar,
-(void) awakeFromNib {
/* NB the NSTableHeaderView class is changed to be an DataTableHeaderView in IB! */
NSUInteger numberOfColumnsWanted = 5;
for (NSUInteger i=0; i<numberOfColumnsWanted; i++) {
PopUpTableHeaderCell *headerCell;
headerCell = [[PopUpTableHeaderCell alloc] init];
[headerCell addItemWithTitle:@"item 1"];
[headerCell addItemWithTitle:@"item 2"];
[headerCell addItemWithTitle:@"item 3"];
NSTableColumn *column;
[column setHeaderCell:headerCell];
[column sizeToFit];
[_tableView addTableColumn:column];
}
/* If we don't do this we get a final (space filling) column with an unclickable (dummy) header */
[_tableView sizeLastColumnToFit];
}
除此之外,我还没有弄清楚如何正确更正该区域中的绘图。
Other than that I haven't figured out how to properly correct the drawing in that region.
它似乎是被复制的最后一个单元格的图像。所以我稍微多一点hack-ish方法将是一个额外的列添加一个空白的名称,并有意忽略鼠标点击你的表视图。希望通过设置最后一列的显示属性,你可以使它看起来你想要的方式。
It seems like it's the image of the last cell that is being duplicated. So I slightly more hack-ish approach would be to add a extra column to your table view with a blank name and which intentionally ignores the mouse clicks. Hopefully by setting the display properties of the last column you can make it look the way you want.
我找不到任何NSTableView或NSTableViewDelegate方法,允许控制区域,所以可能任何其他解决方案将非常复杂。我也会对一个很好的解决方案感兴趣,但我希望这让你开始!
I couldn't find any NSTableView or NSTableViewDelegate method that allow control of this region, so may any other solution would be very complicated. I would be interested in a nice solution too, but I hope this gets you started!
这篇关于使用NSPopUpButtonCell时,在NSTableView中获取重复的标题按钮单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!