2010-10-17 226 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

相关问题