2011-03-22 67 views
1

我在使用表视图显示数据源中的数据时遇到问题。希望有人能在这里指出我正确的方向。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是发送的对象不应该改变吗?我知道IB中的NSTableViews连接正确(请参阅下面的代码),因为我可以在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的闲置事件,其中控制器与对象沟通,对方表示正在发生的任何变化有点模糊。我是否必须将文件列表声明为要观看的数组?

回答

3

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

你也可以使用绑定来连接tableview,然后当你更新它绑定的属性(更多信息here和一个很好的教程here)时它会得到通知。

+0

很高兴你回答,我只是想出了自己,并发布了一个答案,但现在我可以泵一个信用。 :D – PruitIgoe 2011-03-22 17:49:42

+0

+ 1为教程链接 - 我的老板只是说如果你能解释绑定到我你(我)会摇滚......:D – PruitIgoe 2011-03-22 17:50:11

+0

很高兴帮助!绑定在工作时就像魔术一样,但他们的学习曲线肯定会更陡。 – Laura 2011-03-22 18:25:05