gpt4 book ai didi

cocoa - NSTableView 不显示项目

转载 作者:行者123 更新时间:2023-12-03 16:59:34 25 4
gpt4 key购买 nike

我在让 TableView 显示数据源中的数据时遇到问题。希望有人能在这里指出我正确的方向。

在IB中我有两个 TableView ,我分别将它们的标签设置为1和2。我还有一个 AppController 对象,并将两个表连接到数据源和委托(delegate)的表。

该应用程序的工作方式是用户单击一个按钮,该按钮运行 AppController.m 中的一个方法,允许用户导航到一个目录并选择它,该方法然后获取该目录中的所有文件名,然后过滤 .tif和 .eps 文件。非常基本的东西。这是该方法。

//allows the user to select a folder to read and filter for acceptable image files (eps, tif)
- (IBAction) getFileNames : (id) sender {

//clear out file list
if ([fileList count] > 0) {
[fileList removeAllObjects];
}

// Create the File Open Dialog class.
NSOpenPanel* openDlg = [NSOpenPanel openPanel];

// Enable the selection of files in the dialog.
[openDlg setCanChooseFiles:YES];

// Enable the selection of directories in the dialog.
[openDlg setCanChooseDirectories:YES];

// Display the dialog. If the OK button was pressed,
if ( [openDlg runModalForDirectory:nil file:nil] == NSOKButton ) {

// Get an array containing the full filenames of all
// files and directories selected.
NSArray* files = [openDlg filenames];

// Loop through all the files and process them. (Hopefully most of the time Mark will be selecting just the archive directory on storage server)
for(int i = 0; i < [files count]; i++ ) {
//make string of object path
NSString* objPath = [files objectAtIndex:i];

//determine if object is a directory or file
//create a file manager
NSFileManager* fileManager = [[NSFileManager alloc] init];

//set a bool
BOOL isDir;

//check to see if the object is a folder
if ([fileManager fileExistsAtPath:objPath isDirectory:&isDir] && isDir) {

//set the fileList array


//if it is a directory then read the contents of the directory and display in the file list table
NSError* dirErr;
NSMutableArray *rawFileList = (NSMutableArray *)[[NSFileManager defaultManager] contentsOfDirectoryAtPath:objPath error:&dirErr];

//loop through file list and get the file type, if it is a .eps or .tif then move it to fileList
for(int f=0; f<[rawFileList count]; f++) {

//set an NSString
NSString* thisFile = [rawFileList objectAtIndex:f];

//filter out any file that does not have a .tif or .eps extension
if ([thisFile rangeOfString:@".eps"].location != NSNotFound || [thisFile rangeOfString:@".eps"].location != NSNotFound) {
//add correct files to fileList arrays
[fileList addObject:thisFile];
}

//use core graphics to get the meta data of the file and determine if it is an eps or tif file
/*CGImageSourceRef source = CGImageSourceCreateWithURL( (CFURLRef) URL, NULL);
NSDictionary* metadata = (NSDictionary *)CGImageSourceCopyPropertiesAtIndex(source,0,NULL);*/
}

NSLog(@"%i:::", [fileList count]);

//dump file list into NSTable

} else {
NSLog(@"nope, this is a file");
}
}
}

}

当我记录数组 fileList 中的项目数时,我得到 118。但是,当我在表方法中记录 fileList 时,我得到 0。我希望当应用程序“唤醒”但在发送 fileList 之后,对象应该'它改变了吗?我知道 NSTableViews 在 IB 中连接正确(参见下面的代码),因为我可以在 numberOfRowsInTableView 方法中硬编码一个数字,并在 tableView 方法中硬编码一个字符串,并且它们可以工作。他们只是无法与我的阵列一起使用。

- (int)numberOfRowsInTableView:(NSTableView *)t {

NSLog(@"%i", [fileList count]);
return [fileList count];
}

- (id)tableView:(NSTableView *)t objectValueForTableColumn:(NSTableColumn *)tableColumn row:(int)row {
// return [fileList objectAtIndex:row];

if ([t tag] == 1) {
return [fileList objectAtIndex:row];
} else {
return @"table 2";
}

return 0;
}

我对委托(delegate)的工作方式有点模糊,但从文档来看,这似乎与 AppleScript 的空闲事件类似,其中 Controller 和对象相互通信正在发生的任何更改。我是否必须将文件列表声明为要观看的数组?

最佳答案

NSTableView 对象不会主动寻找底层数据模型的更改,因此如果您更改数据模型(在您的情况下是 fileList 数组),那么您需要告诉 TableView 它已更改。加载文件后,您可以在 TableView 上调用 -(void)reloadData,它应该使用 fileList 中的新数据。

您还可以使用绑定(bind)来连接 TableView ,然后当您更新其绑定(bind)到的属性时,它将收到通知(更多信息 here 和一个很好的教程 here )。

关于cocoa - NSTableView 不显示项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5395382/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com