当窗口在焦点上时,Cocoa应用程序滚动速度很慢,而不是

时间:2016-10-27 20:43:34

标签: macos cocoa

我有一个带有包含NSTableView的窗口的cocoa应用程序。每行有几列,三个收音机盒和两个按钮。目前该表有260行,当窗口聚焦时,在表格视图中滚动是非常缓慢和抖动。当窗口没有聚焦时,我将鼠标悬停在桌面视图上并滚动它的黄油光滑。

我试图通过更改背景图并使CoreAnimation Layer无效来解决性能下降的问题。

为什么当窗口没有聚焦时滚动速度会很快,但是当窗口没有聚焦时,滚动会很慢?

我只是感到困惑,为什么滚动如此慢。

这是我的ProposalTableViewController.h

#import <Cocoa/Cocoa.h>
#import <Foundation/Foundation.h>
#import <QuickLook/QuickLook.h>
#import <Quartz/Quartz.h>

@interface ProposalTableViewController : NSObject<NSTableViewDataSource, NSTableViewDelegate, QLPreviewPanelDelegate, QLPreviewPanelDataSource>{
@public
    NSMutableArray *list;
    IBOutlet NSTableView *tableView;
    IBOutlet NSSearchField *searchText;
    IBOutlet NSTextField *countTextField;
}

@property (strong) QLPreviewPanel *previewPanel;

+ (ProposalTableViewController *)getInstance;

- (IBAction)deleteRow:(id)sender;
- (IBAction)exportData:(id)sender;
- (void)loadData;
- (void)countItems;

@end

我的ProposalTableViewController.m

#import "ProposalTableViewController.h"
#import "Proposal.h"
#import "StatusRadioView.h"
#import "DBManager.h"
#import "filePathButtonView.h"
#import <Quartz/Quartz.h>
#import "AppDelegate.h"
@implementation Proposal (QLPreviewItem)

- (NSURL *)previewItemURL
{
    return [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@.pdf",self.filePath]];
}

- (NSString *)previewItemTitle
{
    return [NSString stringWithFormat:@"Proposal %@",self.proposalNumber];
}

@end

@implementation ProposalTableViewController

static ProposalTableViewController *instance;
+ (ProposalTableViewController *)getInstance{
    return instance;
}

//------------------------------------------------------------------------------------------------------------------------------------
//                                                      Init
//------------------------------------------------------------------------------------------------------------------------------------
- (id)init{
    self = [super init];
    if(self){
        instance = self;

        list = [[NSMutableArray alloc] init];
        [self loadData];

        for (NSTableColumn *tableColumn in tableView.tableColumns ) {
            NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:tableColumn.identifier ascending:YES selector:@selector(compare:)];
            [tableColumn setSortDescriptorPrototype:sortDescriptor];
        }
    }
    return self;
}
//------------------------------------------------------------------------------------------------------------------------------------
//                                                      Load Data from SQLite
//------------------------------------------------------------------------------------------------------------------------------------
- (void)loadData {
    list = [[DBManager getProposalTable] select:@""];
    NSSortDescriptor* desc = [[NSSortDescriptor alloc] initWithKey:@"proposalNumber" ascending:NO selector:@selector(compare:)];
    [list sortUsingDescriptors:[NSArray arrayWithObjects:desc, nil]];
    [tableView reloadData];
    [self countItems: list];
}
//------------------------------------------------------------------------------------------------------------------------------------
//                                                      Number of Rows in Table View
//------------------------------------------------------------------------------------------------------------------------------------
- (NSInteger) numberOfRowsInTableView:(NSTableView *)tableView {
    return [list count];
}

-(void)countItems:(NSArray *)list; {
    NSString *countText;
    int size = [list count];
    if (size == 1){
        countText = @"item in list";
    }else{
        countText = @"items in list";
    }
    NSString *itemCount = [NSString stringWithFormat:@"%d %@", size, countText];
    [countTextField setStringValue:itemCount];
}
//------------------------------------------------------------------------------------------------------------------------------------
//                                                      Get View for Table Column
//------------------------------------------------------------------------------------------------------------------------------------
- (NSView *)tableView:(NSTableView *)table_view viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
    Proposal *p = [list objectAtIndex:row];
    NSString *identifier = [tableColumn identifier];
    NSString *holdingValue;

    NSTableCellView *cell = [table_view makeViewWithIdentifier:identifier owner:self];
    if([identifier isEqualToString:@"status"]){
        StatusRadioView *radioView = [[StatusRadioView alloc] initWithProposal:p];
        return radioView;
    }else if ([identifier isEqualToString:@"filePath"]){
        filePathButtonView *buttonView = [[filePathButtonView alloc]initWithProposal:p];
        return buttonView;
    }else if ([identifier isEqualToString:@"clientAccessPoint"]){
        holdingValue = [p valueForKey:identifier];
            if (!holdingValue){
                cell.textField.stringValue = @"N/A";
            }else{
                cell.textField.stringValue = [p valueForKey:identifier];
            }
    }else{
    cell.textField.stringValue = [p valueForKey:identifier];
    }
    return cell;
}

//------------------------------------------------------------------------------------------------------------------------------------
//                                                      Sort Descriptors did Change
//------------------------------------------------------------------------------------------------------------------------------------
-(void)tableView:(NSTableView *)mtableView sortDescriptorsDidChange:(NSArray *)oldDescriptors {
    [list sortUsingDescriptors: [mtableView sortDescriptors]];
    [tableView reloadData];
}
//table row height
-(CGFloat)tableView:(NSTableView *)tableView heightOfRow:(NSInteger)row {
    return 25;
}
//------------------------------------------------------------------------------------------------------------------------------------
//                                                      Search Text did Change
//------------------------------------------------------------------------------------------------------------------------------------
- (void)controlTextDidChange:(NSNotification *)notification {
    NSSortDescriptor* desc = [[NSSortDescriptor alloc] initWithKey:@"proposalNumber" ascending:NO selector:@selector(compare:)];
    NSTextField *textField = [notification object];
    NSString *str = [textField stringValue];
    list = [[DBManager getProposalTable] select:str];
    [list sortUsingDescriptors:[NSArray arrayWithObjects:desc, nil]];
    [tableView reloadData];
    [self countItems: list];
}
//------------------------------------------------------------------------------------------------------------------------------------
//                                                      Export DATA
//------------------------------------------------------------------------------------------------------------------------------------
- (IBAction)exportData:(id)sender{
    NSString *content = @"";
    for(Proposal *p in list){
        NSString *row = [NSString stringWithFormat:@"%@,%@,%@,%@,%@,%@,%@,%@,%@,%@", p.proposalNumber,p.itemNumber,p.clientName,p.medium,p.support,p.cost,p.dateCreated,p.status,p.dateStatusChanged,p.clientAccessPoint];
        row = [row stringByReplacingOccurrencesOfString:@"\n" withString:@""];
        row = [NSString stringWithFormat:@"%@\n", row];
        content = [content stringByAppendingString:row];
    }
    //get the documents directory:
    NSString *documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"Baumgartner Fine Art Restoration"];
    NSString *fileName = [documentsDirectory stringByAppendingPathComponent:@"proposalBuilderDatabase.csv"];

    [[NSFileManager defaultManager] createDirectoryAtPath:documentsDirectory
                              withIntermediateDirectories:NO
                                               attributes:nil
                                                    error:nil];
    [content writeToFile:fileName
              atomically:NO
                encoding:NSStringEncodingConversionAllowLossy
                   error:nil];

    NSAlert *alert = [[NSAlert alloc] init];
    [alert setMessageText:@"Export Succeeded"];
    [alert addButtonWithTitle:@"Ok"];
    [alert runModal];
}
//------------------------------------------------------------------------------------------------------------------------------------
//                                                      Delete row from DB and table
//------------------------------------------------------------------------------------------------------------------------------------
- (IBAction)deleteRow:(id)sender{
    NSString *row = list[tableView.selectedRow];
    NSString *mid = [row valueForKey:@"m_id"];

    ProposalTable *deleteRow = [[ProposalTable alloc] init];

    [deleteRow deleteWithId: mid];
    [self loadData];
    [tableView reloadData];
}
//------------------------------------------------------------------------------------------------------------------------------------
//                                                      QuickLook
//------------------------------------------------------------------------------------------------------------------------------------
- (NSInteger)numberOfPreviewItemsInPreviewPanel:(QLPreviewPanel *)panel {
    return [list count];
}

- (id <QLPreviewItem>)previewPanel:(QLPreviewPanel *)panel previewItemAtIndex:(NSInteger)index {
    return list[tableView.selectedRow];
}

- (BOOL)previewPanel:(QLPreviewPanel *)panel handleEvent:(NSEvent *)event {
    // redirect all key down events to the table view
    if ([event type] == NSKeyDown) {
        [tableView keyDown:event];
        return YES;
    }
    return NO;
}

// This delegate method provides the rect on screen from which the panel will zoom.
- (NSRect)previewPanel:(QLPreviewPanel *)panel sourceFrameOnScreenForPreviewItem:(id <QLPreviewItem>)item {
    return NSZeroRect;
}

- (BOOL)tableView:(NSTableView *)tableView shouldSelectRow:(NSInteger)rowIndex {
    [[QLPreviewPanel sharedPreviewPanel]reloadData];
    return YES;
}
@end

这是我的ProposalTableView.h

#import <Cocoa/Cocoa.h>

@interface ProposalTableView : NSTableView

@end

我的ProposalTableView.m

#import "ProposalTableView.h"
#import "AppDelegate.h"
@implementation ProposalTableView

- (void)drawRect:(NSRect)dirtyRect {
    [super drawRect:dirtyRect];
    self.wantsLayer = YES;

    // Drawing code here.
}


- (void)keyDown:(NSEvent *)theEvent
{
    NSString *key = [theEvent charactersIgnoringModifiers];
    if ([key isEqual:@" "])
    {
        [[NSApp delegate] togglePreviewPanel:self];
    }
    else
    {
        [super keyDown:theEvent];
    }
}
@end

还有一些代码可以建立SqLite数据库连接并与数据库交互以获取记录或删除等...但这里并不需要...我还有代码来绘制单选按钮和其他按钮但是再说一遍,除非有人认为图纸正在减速,否则我认为没有必要......?

1 个答案:

答案 0 :(得分:0)

事实证明,单选按钮的绘制是导致减速的原因......回到绘图板。