我有一个基于文档的应用程序,它有两个类,myDocument和Person。界面包括用于显示数据的表视图和用于在表中添加和删除peson的两个按钮。 myDocument类是数据源和委托。我已经实现了必要的委托方法,但是当我单击按钮向表中添加记录时,表视图不会显示更改。有人能告诉我我做错了吗?
#import "MyDocument.h"
@implementation MyDocument
- (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.
_dataArray = [[NSMutableArray alloc]init];
}
return self;
}
-(IBAction)addPerson:(id)sender
{
Person *newPerson = [[Person alloc] init];
[_dataArray addObject:newPerson];
NSLog(@" how many objects are in the table? %d",[_dataArray count]);
//[newPerson release];
[_tableView reloadData];
}
-(int)numberOfRowsInTableView:(NSTabView*)aTableView
{
return [_dataArray count];
}
- (void)tableView:(NSTableView *)aTableView
willDisplayCell:(id)aCell
forTableColumn:(NSTableColumn *)aTableColumn
row:(NSInteger)rowIndex
{
[_dataArray objectAtIndex:rowIndex];
NSLog(@" this is the object? %@",[_dataArray objectAtIndex:rowIndex]);
[_tableView reloadData];
}
- (id) tableView:(NSTableView *)aTableView
objectValueForTableColumn:(NSTableColumn *)aTableColumn
row:(int)rowIndex
{
//NSLog(@"this is the object %@",[array objectAtIndex:rowIndex]);
NSLog(@"this is what is in the array %@",[_dataArray objectAtIndex:rowIndex]);
return [_dataArray objectAtIndex:rowIndex];
//return [array replaceObjectAtIndex:[tableView selectedRow ] withObject: @"Micheal jordan"];
}
- (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
答案 0 :(得分:0)
检查_tableView插座是否实际被初始化为指向表视图。