我需要帮助搞清楚如何显示一个NSArray的内容为NSTableView的。我的NSArray充满了(或者至少我认为它是)来自目录的文件名。我使用NSFileManager获取目录中文件的名称,然后将该信息加载到NSArray中。但我无法弄清楚如何将NSArray加载到NSTableView中。如何添加文件名到NSTableView的的一个NSArray? - 可可
AppDelegate.h
#import <Cocoa/Cocoa.h>
@interface AppDelegate : NSObject <NSApplicationDelegate> {
IBOutlet NSTableView *tableView;
NSArray *list;
IBOutlet NSTextField *text;
NSFileManager *manager;
NSString *path;
NSString *pathFinal;
}
@property (assign) IBOutlet NSWindow *window;
- (IBAction)listArray:(id)sender;
@end
AppDelegate.m
#import "AppDelegate.h"
@implementation AppDelegate
@synthesize window = _window;
- (int)numberOfRowsInTableView:(NSTableView *)tableView
{
return [list count];
}
- (id)tableView:(NSTableView *)tableView
objectValueForTableColumn:(NSTableColumn *)tableColumn
row:(int)row
{
return [list objectAtIndex:row];
}
- (IBAction)listArray:(id)sender {
path = @"~/Library/Application Support/minecraft/bin/";
pathFinal = [path stringByExpandingTildeInPath];
list = [manager directoryContentsAtPath:pathFinal];
[tableView reloadData];
}
- (void)dealloc
{
[super dealloc];
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
}
@end
我遇到了麻烦这项工作。它只是没有做任何事情,我在日志中没有遇到任何错误。当我放入NSTableDataSource协议时,它说它没有找到,并告诉我将其更改为NSTableViewDataSource,我不知道是否是这个问题。我把表格设置为IB和委托人的数据源,但没有发生任何事情。它还说,directoryContentsAtPath已被弃用。 – drewsdunne 2012-02-19 18:51:44
对不起,这是我的协议名称错误,它是'NSTableViewDataSource'。您不要将表视图设置为数据源,您将'AppController'对象设置为表视图的数据源。将表视图的'datasource'插座连接到'AppController'实例。 'directoryContentsAtPath' ***被***弃用。如果你看看文档,它会告诉你使用'contentsOfDirectoryAtPath:error:'来代替,所以这样做。 – 2012-02-20 03:48:25
对不起,我犯了一个错误,我误解了这一点。我确实将表视图的'datasource'连接到'AppController'。但它仍然没有在表格中显示任何内容,并且表示没有'contentsOfDirectoryAtPath这样的实例方法:错误:' – drewsdunne 2012-02-20 04:34:30