NSTableView没有填充

时间:2012-05-24 19:10:15

标签: cocoa nsarray nstableview

我一直在努力让这个NSTableView在过去7个小时内填充。我试图获取所有当前正在运行的应用程序的列表,并将它们放入NSTableView。最终我想解析结果并将PID组织在一列中,将Application Bundle组织在另一列中。我在“return [listOfWindows objectAtIndex:row];”上收到EXC_BAD_ACCESS错误我目前正在使用Xcode 4.3.2并运行OS X Lion 10.7.4。提前谢谢大家!

@interface AppDelegate : NSObject <NSApplicationDelegate>
{
    IBOutlet NSMenu *statusMenu;
    IBOutlet NSButton *button;
    IBOutlet NSWindow *menuWindow;
    IBOutlet NSTableView *proTable;
    NSArray *listOfWindows;
    IBOutlet NSArrayController *arrayController;
    AppDelegate *mainMenu;
    NSWorkspace  *workSpace;

    NSStatusItem *statusItem;
}

@property (assign) IBOutlet NSWindow *window;

-(IBAction)loadConfig:(id)sender;
@end

#import "AppDelegate.h"
@implementation AppDelegate
@synthesize window = _window;


- (void) awakeFromNib
{   
[[NSDistributedNotificationCenter defaultCenter] addObserver:self
                                                                         selector:@selector(loadMenu:) 
                                                              name:@"WhiteBox"
                                                      object:nil];
[self addStatusItem];

 //[proTable setDataSource:self];

listOfWindows = [[NSWorkspace sharedWorkspace] runningApplications];
NSLog(@"index %@", listOfWindows);

int y = 0;
y = [listOfWindows count];
NSLog(@"y = %d", y);

[proTable setAllowsMultipleSelection:YES];   
    }

-(void)applicationWillTerminate
{
    NSLog(@"Will Terminate");
}


- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{

}


-(void)applicationDidResignActive:(NSNotification *)notification
{
    NSLog(@"Resign Active");

}

-(void) addStatusItem
{
    //Create a variable length status item from the system statusBar
    statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength];
    [statusItem retain];

//Set a Title for it
[statusItem setTitle:@"Status Item"];

    //Set an Image and an alternate image
    //[statusItem setImage:[NSImage imageNamed:@"lnc"]];
    //[statusItem setAlternateImage: [NSImage imageNamed:@"status"]];

    //Add a Tool Tip
    [statusItem setToolTip:@"Status Item Tooltip"];

    //Choose to highlight the item when clicked
    [statusItem setHighlightMode:YES];

    //To Trigger a method on click use the following two lines of code
[statusItem setMenu:statusMenu];
    //[statusItem setAction:@selector(loadMenu:)];

}

-(IBAction)loadConfig:(id)sender
{

    if(! [menuWindow isVisible] )
    {
        [menuWindow makeKeyAndOrderFront:sender];
    } else {
        [menuWindow performClose:sender];
    }

}


- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView
{
    return [listOfWindows count];
}
- (id)tableView:(NSTableView *)tableView 
objectValueForTableColumn:(NSTableColumn *)tableColumn 
            row:(NSInteger)row
{
    return [listOfWindows objectAtIndex:row];
}



@end

2 个答案:

答案 0 :(得分:0)

表视图的数据源是什么对象?我没有在您发布的源代码中看到任何实现NSTableViewDataSource protocol的对象。

此外,您是否尝试在各种数据源方法中放置断点以查看调试器是否在其中停止?如果没有,通常表明您的数据源未连接到表格视图。

答案 1 :(得分:0)

我得到了: - [NSRunningApplication copyWithZone:]:运行代码时无法识别的选择器错误。这可以通过在tableView中更改返回行来修复:objectValueForTableColumn:row:to

return [[listOfWindows objectAtIndex:row] localizedName];

NSRunningApplication不符合NSCopying,所以我不知道你是否可以将该类的实例放在表视图中。但是,您可以获取其属性,如localizedName,processIdentifier和bundleIdentifier。

我之前遇到过没有实现NSCopying的类的问题,我很高兴知道是否有人知道在表视图或大纲视图中使用这些类的方法。

相关问题