2010-10-17 221 views
1

我正在構建一個基於文檔的Mac應用程序。我有兩個類myDocument和Person。我遇到的困難是當我按下按鈕在表格視圖中添加新的人物並顯示它時,它不會顯示在表格視圖中。我已經在委託方法中放置了日誌語句。由於我的日誌語句沒有顯示在控制檯中,我知道他們沒有被調用。下面是委託方法爲什麼我的表視圖委託方法不被調用?

- (int)numberOfRowsInTableView:(NSTableView *)aTableView 
    { 
     return [employees count]; 
    } 

    - (id)tableView:(NSTableView *)aTableView 
    objectValueForTableColumn:(NSTableColumn *)aTableColumn 
       row:(int)rowIndex 
    { 
     // What is the identifier for the column? 
     NSString *identifier = [aTableColumn identifier]; 
     NSLog(@"the identifier's name is : %s",identifier); 
     // What person? 
     Person *person = [employees objectAtIndex:rowIndex]; 

     // What is the value of the attribute named identifier? 
     return [person valueForKey:identifier]; 
    } 

    - (void)tableView:(NSTableView *)aTableView 
     setObjectValue:(id)anObject 
     forTableColumn:(NSTableColumn *)aTableColumn 
        row:(int)rowIndex 
    { 
     NSString *identifier = [aTableColumn identifier]; 
     Person *person = [employees objectAtIndex:rowIndex]; 
     NSLog(@"inside the setObjectMethod: %@",person); 

     // Set the value for the attribute named identifier 
     [person setValue:anObject forKey:identifier]; 

    [tableView reloadData]; 
} 

這裏的實現是我的.xib的PIC alt text

這裏是我的行動方法

#pragma mark Action Methods 
-(IBAction)createEmployee:(id)sender 
{ 
    Person *newEmployee = [[Person alloc] init]; 
    [employees addObject:newEmployee]; 
    [newEmployee release]; 
    [tableView reloadData]; 
    NSLog(@"the new employees name is : %@",[newEmployee personName]); 
} 
-(IBAction)deleteSelectedEmployees:(id)sender 
{ 
    NSIndexSet *rows = [tableView selectedRowIndexes]; 

    if([rows count] == 0){ 
     NSBeep(); 
     return; 
    } 
    [employees removeObjectAtIndexs:rows]; 
    [tableView reloadData]; 

}

+0

您不需要將'reloadData'發送到'tableView:setObjectValue:forTableColumn:row:'中的表視圖,因爲它是發送該消息的表視圖 - 它已經知道已更改的值。它會在之後再次重新查詢,以防萬一您替換了其他的東西。 – 2010-10-17 03:14:20

+0

請編輯您的問題以包含'createEmployee:'的實現。 – 2010-10-17 03:15:16

+0

我添加了createEmployee的實現 – lampShade 2010-10-17 03:35:35

回答

5

你忘了綁定文檔的tableView出口到實際的表格視圖。因此你的reloadData消息被髮送到nil。

+1

每當我添加新的動作/插座代碼,我把NSAssert(outlet!= nil);在路徑的某個地方。因爲更多的時候我忘記連接ib中的所有連接。 – 2010-10-17 14:23:43

相關問題