NSTableView不显示项目

时间:2011-03-22 17:29:14

标签: cocoa nstableview

我在获取表视图时遇到问题,无法显示数据源中的数据。希望有人可以在这里指出我正确的方向。

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

该应用程序通过用户点击按钮来运行,该按钮在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; 
} 

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

1 个答案:

答案 0 :(得分:3)

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

您还可以使用绑定来连接tableview,然后在更新绑定的属性时会收到通知(更多信息here和一个好的教程here)。