2011-03-22 70 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