NSTableView中的按钮 - 需要更好的方法

时间:2016-10-31 18:27:59

标签: macos cocoa

我有一个NSTableView,其中每行有两列包含按钮。这些按钮会破坏性能并使滚动无法忍受。有没有办法纠正这个?这是我做这个的最佳方式(我知道答案可能不是......)我需要第一列中的单选按钮,但第二列大部分都被黑客攻击,可能会更好(它可能是#s; s目前有一组两个单选按钮,看起来像常规按钮)

下面是绘制第一列按钮的代码。

StatusRadioView.h

//  StatusRadioView.h    
#import <Cocoa/Cocoa.h>
#import "Proposal.h"
@interface StatusRadioView : NSView
- (id)initWithProposal:(Proposal *)proposal;
@end

StatusRadioView.m

//  StatusRadioView.m  
#import "StatusRadioView.h"
#import "DBManager.h"

#define STR_PENDING  @"Pending"
#define STR_APPROVED @"Approved"
#define STR_REJECTED @"Rejected"

@implementation StatusRadioView{
    NSMatrix *matrix;
    Proposal *proposal;
}
//------------------------------------------------------------------------------------------------------------------------------------
//                                                              Init
//------------------------------------------------------------------------------------------------------------------------------------
- (id)init{
    self = [super init];
    if(self){
        [self drawUI];
    }
    return self;
}
//------------------------------------------------------------------------------------------------------------------------------------
//                                                          Init with Frame
//------------------------------------------------------------------------------------------------------------------------------------
- (id)initWithFrame:(NSRect)frameRect{
    self = [super initWithFrame:frameRect];
    if(self){
        [self drawUI];
    }
    return self;
}
//------------------------------------------------------------------------------------------------------------------------------------
//                                                         Init with Coder
//------------------------------------------------------------------------------------------------------------------------------------
- (id)initWithCoder:(NSCoder *)coder{
    self = [super initWithCoder:coder];
    if(self){
        [self drawUI];
    }
    return self;
}
//------------------------------------------------------------------------------------------------------------------------------------
//                                                      Init with Proposal
//------------------------------------------------------------------------------------------------------------------------------------
- (id)initWithProposal:(Proposal *)aProposal{
    self = [super init];
    proposal = aProposal;
    if(self){
        [self drawUI];
    }
    return self;
}
//------------------------------------------------------------------------------------------------------------------------------------
//                                                      Draw UI
//------------------------------------------------------------------------------------------------------------------------------------
- (void)drawUI{
    self.frame = NSMakeRect(0, 0, 250, 50);
    NSButtonCell *proto = [[NSButtonCell alloc] init];
    [proto setTitle:@"Options    "];
    [proto setButtonType: NSRadioButton];

    //define the matrix size where you'll put the radio buttons
    NSRect matrixRect = NSMakeRect(0.0,0,250.0, 22.0);

    matrix = [[NSMatrix alloc] initWithFrame: matrixRect
                                                 mode: NSRadioModeMatrix
                                            prototype: (NSCell *)proto
                                         numberOfRows:1 numberOfColumns:3];

    [self addSubview: matrix];

    //set the radio buttons' titles by getting references to the matrix's cells
    NSArray *cells = [matrix cells];
    [[cells objectAtIndex:0] setTitle:STR_PENDING];
    [[cells objectAtIndex:0] setFont:[NSFont systemFontOfSize:11.0]];

    [[cells objectAtIndex:1] setTitle:STR_APPROVED];
    [[cells objectAtIndex:1] setFont:[NSFont systemFontOfSize:11.0]];

    [[cells objectAtIndex:2] setTitle:STR_REJECTED];
    [[cells objectAtIndex:2] setFont:[NSFont systemFontOfSize:11.0]];

    [matrix setTarget:self];
    [matrix setAction:@selector(selectedAction:)];
    if(proposal){
        if([proposal.status isEqualToString:STR_PENDING]){
            [matrix selectCell:[cells objectAtIndex:0]];
        }else if([proposal.status isEqualToString:STR_APPROVED]){
            [matrix selectCell:[cells objectAtIndex:1]];
        }else if([proposal.status isEqualToString:STR_REJECTED]){
            [matrix selectCell:[cells objectAtIndex:2]];
                    }
    }
}
//------------------------------------------------------------------------------------------------------------------------------------
//                                                      Radio Button Clicked
//------------------------------------------------------------------------------------------------------------------------------------
-(void) selectedAction:(id) sender{
    NSInteger col = [sender selectedColumn];
    if(col==0){
        proposal.status = STR_PENDING;
    }else if(col==1){
        proposal.status = STR_APPROVED;
    }else if(col==2){
        proposal.status = STR_REJECTED;
    }
    [[DBManager getProposalTable] updateWithProposal:proposal]; 
}
@end

第二列的代码与另一组按钮:

filePathViewButton.h

#import <Cocoa/Cocoa.h>
#import "Proposal.h"
#import "ProposalTableViewController.h"
@interface filePathButtonView : NSView
- (id)initWithProposal:(Proposal *)proposal;
@end

filePathViewButton.m

#import "filePathButtonView.h"
#import "DBManager.h"

#import "ProposalTableViewController.h"
#import "Proposal.h"

#define STR_DOCX  @"Word"
#define STR_PDF @"PDF"
#define STR_PHOTO @"Photo"


@implementation filePathButtonView{
    NSMatrix *matrix;
    Proposal *proposal;
}

//------------------------------------------------------------------------------------------------------------------------------------
//                                                              Init
//------------------------------------------------------------------------------------------------------------------------------------
- (id)init{
    self = [super init];
    if(self){
        [self drawUI];
    }
    return self;
}
//------------------------------------------------------------------------------------------------------------------------------------
//                                                          Init with Frame
//------------------------------------------------------------------------------------------------------------------------------------
- (id)initWithFrame:(NSRect)frameRect{
    self = [super initWithFrame:frameRect];
    if(self){
        [self drawUI];
    }
    return self;
}
//------------------------------------------------------------------------------------------------------------------------------------
//                                                         Init with Coder
//------------------------------------------------------------------------------------------------------------------------------------
- (id)initWithCoder:(NSCoder *)coder{
    self = [super initWithCoder:coder];
    if(self){
        [self drawUI];
    }
    return self;
}
//------------------------------------------------------------------------------------------------------------------------------------
//                                                      Init with Proposal
//------------------------------------------------------------------------------------------------------------------------------------
- (id)initWithProposal:(Proposal *)aProposal{
    self = [super init];
    proposal = aProposal;
    if(self){
        [self drawUI];
    }
    return self;
}
//------------------------------------------------------------------------------------------------------------------------------------
//                                                      Draw UI
//------------------------------------------------------------------------------------------------------------------------------------
- (void)drawUI{
    self.frame = NSMakeRect(0, 0, 200, 25);
    NSButtonCell *proto = [[NSButtonCell alloc] init];
    [proto setTitle:@"file    "];
    //[proto setButtonType: NSMomentaryChangeButton];
    [proto setBezelStyle:NSInlineBezelStyle];
    //[proto setBezelStyle:NSRegularSquareBezelStyle];
    [proto setBordered:YES];

    //define the matrix size where you'll put the buttons
    NSRect matrixRect = NSMakeRect(0.0,2.5,250.0, 20);

    matrix = [[NSMatrix alloc] initWithFrame: matrixRect
                                        mode: NSRadioModeMatrix
                                   prototype: (NSCell *)proto
                                numberOfRows:1 numberOfColumns:2];

    [self addSubview: matrix];

    //set the radio buttons' titles by getting references to the matrix's cells
    NSArray *cells = [matrix cells];
    [[cells objectAtIndex:0] setTitle:STR_DOCX];
    [[cells objectAtIndex:0] setFont:[NSFont systemFontOfSize:11.0]];

    [[cells objectAtIndex:1] setTitle:STR_PHOTO];
    [[cells objectAtIndex:1] setFont:[NSFont systemFontOfSize:11.0]];


    [matrix setTarget:self];
    [matrix setAction:@selector(selectedAction:)];
}
//------------------------------------------------------------------------------------------------------------------------------------
//                                                      Button Clicked
//------------------------------------------------------------------------------------------------------------------------------------
-(void) selectedAction:(id) sender{
    NSInteger col = [sender selectedColumn];
    NSString *rawFilePath;

    if(col==0){
        rawFilePath = [NSString stringWithFormat:@"%@%@", proposal.filePath, @".docx"];
        [[NSWorkspace sharedWorkspace]openFile:rawFilePath];
    }else if(col==1){
        rawFilePath = [NSString stringWithFormat:@"%@", proposal.photoPath];
        [[NSWorkspace sharedWorkspace] openFile:rawFilePath withApplication:@"Preview"];

    }
}
@end

0 个答案:

没有答案