简体   繁体   English

NSTableView不填充

[英]NSTableView not Populating

I've been trying to get this NSTableView to populate for the last 7 hours. 在过去的7个小时中,我一直在尝试填充此NSTableView。 I am trying to get a list of all the currently running application and put them into an NSTableView. 我正在尝试获取所有当前正在运行的应用程序的列表,并将它们放入NSTableView中。 Eventually I would like to parse the resultes and organize the PID in one column and the Application Bundle in the other. 最终,我想解析结果,并在一列中组织PID,在另一列中组织“应用程序捆绑”。 I am getting an EXC_BAD_ACCESS error on " return [listOfWindows objectAtIndex:row];" 我在“ return [listOfWindows objectAtIndex:row];”上收到EXC_BAD_ACCESS错误。 I am currently using Xcode 4.3.2 and running OS X Lion 10.7.4. 我当前正在使用Xcode 4.3.2并运行OS X Lion 10.7.4。 Thanks in advance everyone! 预先感谢大家!

@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

What object is the table view's data source? 表格视图的数据源是哪个对象? I don't see any object in the source you posted as implementing the NSTableViewDataSource protocol . 在实现NSTableViewDataSource协议时,您发布的源代码中没有看到任何对象。

Further, have you tried putting breakpoints in the various data source methods to see if the debugger stops in them? 此外,您是否尝试过将断点放入各种数据源方法中以查看调试器是否在其中停止? If not, it's usually a good sign that your data source isn't connected to your table view. 如果不是,通常表明您的数据源未连接到表视图是一个好兆头。

I got: -[NSRunningApplication copyWithZone:]: unrecognized selector error when I ran your code. 我得到:-[NSRunningApplication copyWithZone:]:运行我的代码时出现无法识别的选择器错误。 This could be fixed by changing your return line in tableView:objectValueForTableColumn:row: to 可以通过将tableView:objectValueForTableColumn:row:中的返回行更改为来解决此问题。

return [[listOfWindows objectAtIndex:row]localizedName]; 返回[[listOfWindows objectAtIndex:row] localizedName];

NSRunningApplication doesn't conform to NSCopying, so I don't know if you can put instances of that class in a table view. NSRunningApplication不符合NSCopying,所以我不知道您是否可以在表视图中放置该类的实例。 However, you can get its properties like localizedName, processIdentifier, and bundleIdentifier. 但是,您可以获取其属性,例如localizedName,processIdentifier和bundleIdentifier。

I've run into this problem before with classes that don't implement NSCopying, I'd be happy to know if anyone knows a way to use these classes in table views or outline views. 在使用未实现NSCopying的类之前,我曾遇到过这个问题,很高兴知道是否有人知道在表视图或大纲视图中使用这些类的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM