簡體   English   中英

NSTableView 列標題中的復選框

[英]Checkbox in NSTableView column header

我需要在 NSTableView 列標題中添加一個復選框。 我可以在上列的所有行中添加復選框。 但我需要在列標題級別。 我需要執行全選功能,但無法在表頭級別添加一個復選框。 任何示例代碼或想法都會有所幫助。

謝謝,蘇布拉特

創建NSButtonCell子類

@interface CheckboxHeaderCell : NSButtonCell {
    NSButtonCell *cellCheckBox;
    NSColor *bkColor;
}

-(void)setTitle:(NSString *)title;
-(void)setBkColor:(NSColor *)color;
-(BOOL)getState;
-(void)onClick;

@end  

@implementation CheckboxHeaderCell

- (id)init
{
    if (self = [super init])
    {
        bkColor = nil;

        cellCheckBox = [[ NSButtonCell alloc] init];
        [cellCheckBox setTitle:@""];
        [cellCheckBox setButtonType:NSSwitchButton];
        [cellCheckBox setBordered:NO];
        [cellCheckBox setImagePosition:NSImageRight];
        [cellCheckBox setAlignment:NSLeftTextAlignment];
        [cellCheckBox setObjectValue:[NSNumber numberWithInt:0]];

        [cellCheckBox setControlSize:NSSmallControlSize];
        [cellCheckBox setFont:[NSFont systemFontOfSize:[NSFont
                                                        smallSystemFontSize]]];
    }
    return self;
}

- (void)dealloc
{
    [cellCheckBox release];
    [bkColor release];
    [super dealloc];
}

-(void)setTitle:(NSString *)title
{
    [cellCheckBox setTitle:title];
}

-(void)setBkColor:(NSColor *)color
{
    [color retain];
    [bkColor release];
    bkColor = color;
}

-(BOOL)getState
{
    return [[cellCheckBox objectValue] boolValue];
}

-(void)onClick
{
    BOOL state = ![[cellCheckBox objectValue] boolValue];
    [cellCheckBox setObjectValue:[NSNumber numberWithBool:state]];
}

- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
{
    if (bkColor != nil)
        [cellCheckBox setBackgroundColor:bkColor];

    [cellCheckBox drawWithFrame:cellFrame inView:controlView] ;
}

@end 

如何使用:

CheckboxHeaderCell *mHeaderCell = [[CheckboxHeaderCell alloc] init];
    NSTableColumn *checkBoxColumn = [mOutlineView tableColumnWithIdentifier:@"state"];
    [checkBoxColumn setHeaderCell:mHeaderCell];  

- (void)tableView: (NSTableView *)tableView didClickTableColumn:(NSTableColumn *)tableColumn
{
    NSLog(@"didClickTableColumn");
    CheckboxHeaderCell *headerCell = [tableColumn headerCell];
    [headerCell onClick];
}

使用定制NSCell實例不工作的原因很簡單, NSTableHeaderView是坐標的NSHeaderCell實例處理所有的鼠標事件處理。 因此表格標題中的單元格按鈕永遠不會響應鼠標事件。

解決方案是將所需的NSControl實例添加到自定義NSTableHeaderView 自定義子類可以在IB中的table view上設置。 控件被添加到已識別的表列中,如下所示:

[(BPTableHeaderView *)self.tableView.headerView addSubview:self.passwordTableHeaderButton
                                          columnIdentifier:@"password"
                                                 alignment:NSLayoutAttributeRight];

任何NSView實例都可以添加到列標題,盡管添加NSControl實例可能是最常見的。

NSTableHeaderView子類。

@interface BPTableHeaderView : NSTableHeaderView

- (void)addSubview:(NSView *)view columnIdentifier:(NSString *)identifier alignment:(NSLayoutAttribute)alignment;

@end

@interface BPTableHeaderView()

// collections
@property (strong) NSMutableDictionary<NSString *, NSDictionary *> *store;

// primitives
@property (assign, nonatomic) BOOL subviewsVisible;

@end

@implementation BPTableHeaderView

- (id)initWithFrame:(NSRect)frameRect
{
    self = [super initWithFrame:frameRect];
    if (self) {
        [self commonInit];
    }
    return self;
}

- (id)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
         [self commonInit];
    }
    return self;
}

- (void)commonInit
{
    _subviewsVisible = YES;
    _store = [NSMutableDictionary new];
}

#pragma mark -
#pragma mark Accessors

- (void)setSubviewsVisible:(BOOL)subviewsVisible
{
    if (_subviewsVisible == subviewsVisible) return;

    _subviewsVisible = subviewsVisible;

    for (NSString *identifier in self.store.allKeys) {
        NSDictionary *info = self.store[identifier];
        NSView *view = info[@"view"];
        view.hidden = !_subviewsVisible;
    }
}

#pragma mark -
#pragma mark Drawing

- (void)drawRect:(NSRect)dirtyRect {

    [super drawRect:dirtyRect];

    if (self.draggedColumn != -1 || self.resizedColumn != -1) {
        [self layoutSubviews];
    }
}

#pragma mark -
#pragma mark View management

- (void)addSubview:(NSView *)view columnIdentifier:(NSString *)identifier alignment:(NSLayoutAttribute)alignment
{
    self.store[identifier] = @{@"view" : view, @"alignment" : @(alignment)};
    [self addSubview:view];
    self.needsLayout = YES;
}

#pragma mark -
#pragma mark Layout

- (void)layout
{
    [super layout];
    [self layoutSubviews];
}

- (void)layoutSubviews
{
    for (NSString *identifier in self.store.allKeys)
    {
        // info
        NSDictionary *info = self.store[identifier];
        NSView *view = info[@"view"];
        NSLayoutAttribute alignment = [info[@"alignment"] integerValue];

        // views and cells
        NSTableColumn *column = [self.tableView tableColumnWithIdentifier:identifier];
        NSTableHeaderCell *headerCell = column.headerCell;
        NSInteger idx = [self.tableView.tableColumns indexOfObject:column];
        if (idx == NSNotFound) continue;

        // rects
        NSRect headerRect = [self headerRectOfColumn:idx];
        NSRect sortIndicatorRect = NSZeroRect;
        if (column.sortDescriptorPrototype) {
            sortIndicatorRect = [headerCell sortIndicatorRectForBounds:headerRect];
        }

        // position view
        NSPoint viewOrigin = NSMakePoint(0, 0);
        CGFloat y = (headerRect.size.height - view.frame.size.height)/2;
        CGFloat xDelta = 3;
        if (alignment == NSLayoutAttributeLeft) {
            viewOrigin = NSMakePoint(headerRect.origin.x + xDelta, y);
        }
        else {
            viewOrigin = NSMakePoint(headerRect.origin.x + headerRect.size.width - view.frame.size.width - sortIndicatorRect.size.width - 5, y);
        }
        [view setFrameOrigin:viewOrigin];
    }

}
@end

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM