#import "MyDocument.h"
#import "Person.h"
/**
* 表每行的元素放在一个对象中, 所有行存放在一个NSMutableArray中, 这样好管理, 当然每列的内容可以分开存放, 但不方便.
* 当表有多列时, (在IB中选中列)要为每个列指定一个identifier, 其值为存储的对象的属性名称, 当使用
* [aTableColumn identifier]时, 就可以取得这一列存储的内容是这个对象的哪一个属性,
* 然后使用key-value-coding([obj setValue:value forKey:@"name"], [obj valueForKey:@"name"])
* 来取得与修改这个对象的属性.
*
* 表的内容是存储在NSTableDataSource中, 只要一个类实现了下面两个方法, 然后在界面中直接右击表, 把其中的
* dataSource拖动指到doc window中的这个类(文档的直接是File's Owner, 非文档的先建立一个NSObject的Controller)
* - (int)numberOfRowsInTableView:(NSTableView*)tv 返回列的行.
* - (id)tableView:(NSTableView*)tv objectValueForTableColumn:(NSTableColumn*)tc row:(int)rowIndex
*
* 如果想使一个表是可以修改的, 再实现下面的这个方法即可
* - (void)tableView:(NSTableView*)tc setObjectValue:(id)anObject
* forTableColumn:(NSTableColumn*)tc row:(int)rowIndex
*
*
* IBOutlet是一个宏, 给Interface Builder提供信息(即在其中可以看到IBOutlet修饰的变量).
* 当你想使用一个界面元素的方法(如NSTextField)时, 就声明一个相关的变量为IBOutlet,
* 这样, 在你的程序中就可以直接操作那个界面元素的一切.
*
* IBAction实际上就是void, 但是也是为了给IB提供信息, 让IBActon定义的方法在IB中显示出来.
* 事件处理函数用IBAction来定义, 如作为NSButton的action selector.
*
* getter的名字为属性的名字, setter的名字为set加上属性的名字(此时属性的名字的第一个字母要大写).
*/
@implementation MyDocument
- (IBAction)addEmployee:(id)sender {
Person* newEmployee = [[Person alloc] init];
[employees addObject:newEmployee];
[newEmployee release];
[tableView reloadData];
}
- (IBAction)removeEmployee:(id)sender {
NSIndexSet* rows = [tableView selectedRowIndexes];
if ([rows count] == 0) {
NSBeep();
return;
}
[employees removeObjectsAtIndexes:rows];
[tableView reloadData];
}
////////////////////////////////////////////////////////////////////////////////////
// Methods for NSTableDataSource
////////////////////////////////////////////////////////////////////////////////////
- (int)numberOfRowsInTableView:(NSTableView*)tv {
return [employees count];
}
- (id)tableView:(NSTableView*)tv objectValueForTableColumn:(NSTableColumn*)tc
row:(int)rowIndex {
NSString* columnIdentifier = [tc identifier];
NSLog(@"%@", columnIdentifier);
Person* person = [employees objectAtIndex:rowIndex];
return [person valueForKey:columnIdentifier];
//return nil;
}
- (void)tableView:(NSTableView*)tv setObjectValue:(id)anObject
forTableColumn:(NSTableColumn*)tc row:(int)rowIndex {
NSString* identifier = [tc identifier];
Person* employee = [employees objectAtIndex:rowIndex];
[employee setValue:anObject forKey:identifier];
}
////////////////////////////////////////////////////////////////////////////////////
// Overrieded Methods.
////////////////////////////////////////////////////////////////////////////////////
- (id)init
{
self = [super init];
if (self) {
// Add your subclass-specific initialization here.
// If an error occurs here, send a [self release] message and return nil.
employees = [[NSMutableArray alloc] init];
}
return self;
}
- (void)dealloc {
[employees release];
[super dealloc];
}
- (NSString *)windowNibName
{
// Override returning the nib file name of the document
// If you need to use a subclass of NSWindowController or if your document supports multiple NSWindowControllers, you should remove this method and override -makeWindowControllers instead.
return @"MyDocument";
}
- (void)windowControllerDidLoadNib:(NSWindowController *) aController
{
[super windowControllerDidLoadNib:aController];
// Add any code here that needs to be executed once the windowController has loaded the document's window.
}
- (NSData *)dataOfType:(NSString *)typeName error:(NSError **)outError
{
// Insert code here to write your document to data of the specified type. If the given outError != NULL, ensure that you set *outError when returning nil.
// You can also choose to override -fileWrapperOfType:error:, -writeToURL:ofType:error:, or -writeToURL:ofType:forSaveOperation:originalContentsURL:error: instead.
// For applications targeted for Panther or earlier systems, you should use the deprecated API -dataRepresentationOfType:. In this case you can also choose to override -fileWrapperRepresentationOfType: or -writeToFile:ofType: instead.
if ( outError != NULL ) {
*outError = [NSError errorWithDomain:NSOSStatusErrorDomain code:unimpErr userInfo:NULL];
}
return nil;
}
- (BOOL)readFromData:(NSData *)data ofType:(NSString *)typeName error:(NSError **)outError
{
// Insert code here to read your document from the given data of the specified type. If the given outError != NULL, ensure that you set *outError when returning NO.
// You can also choose to override -readFromFileWrapper:ofType:error: or -readFromURL:ofType:error: instead.
// For applications targeted for Panther or earlier systems, you should use the deprecated API -loadDataRepresentation:ofType. In this case you can also choose to override -readFromFile:ofType: or -loadFileWrapperRepresentation:ofType: instead.
if ( outError != NULL ) {
*outError = [NSError errorWithDomain:NSOSStatusErrorDomain code:unimpErr userInfo:NULL];
}
return YES;
}
@end